在Android上使用RectF参数裁剪位图

时间:2022-08-27 10:42:14

Using MikeOrtiz's awesome ImageView implementation with touch and zoom events, I wanted to crop a picture taken with the camera to match the zoom. Using his method...

使用MikeOrtiz的令人敬畏的ImageView实现以及触摸和缩放事件,我想裁剪用相机拍摄的照片以匹配变焦。用他的方法......

// Return a Rect representing the zoomed image.
RectF getZoomedRect();

...I tried cropping the resulting picture bitmap to the zoom size like so:

...我尝试将生成的图片位图裁剪为缩放尺寸,如下所示:

RectF zoomCoordinates = mTouchImageView.getZoomedRect();

Bitmap croppedBitmapToOverview = Bitmap.createBitmap(
                AppResources.sCurrentImage,
                ((int) zoomCoordinates.left),
                ((int) zoomCoordinates.top),
                ((int) zoomCoordinates.width()),
                ((int) zoomCoordinates.height()));

However I get a "must be bigger than 0" error with this. While debugging I noticed ALL values were 0 due to casting to an Integer. The real values however go something like this:

但是我得到一个“必须大于0”的错误。在调试时我注意到由于转换为Integer,所有值都为0。然而真正的价值观是这样的:

//Log.d print for each of those fields without the int cast
Left 0.34047672
Top 0.20797288
Width 0.33333334
Height 0.3429547

So there's my problem, but I can't see how to fix this. I've never worked with bitmaps before or canvas, Rect, etc.

所以这是我的问题,但我看不出如何解决这个问题。我之前从未使用过bitmaps或canvas,Rect等。

Is there some tweaking I could do to these values, or should I take a different approach altogether?

我是否可以对这些值进行一些调整,或者我应该采取不同的方法?

1 个解决方案

#1


0  

Got around the problem by simply taking a "screenshot" of the View of sorts. This got me a Bitmap with the picture as it was zoomed

通过简单地获取各种视图的“屏幕截图”来解决问题。这让我得到了一张带有缩放图片的位图

mTouchImageView.setDrawingCacheEnabled(true);
        AppResources.sCurrentImage = Bitmap.createBitmap(mTouchImageView.getDrawingCache());

#1


0  

Got around the problem by simply taking a "screenshot" of the View of sorts. This got me a Bitmap with the picture as it was zoomed

通过简单地获取各种视图的“屏幕截图”来解决问题。这让我得到了一张带有缩放图片的位图

mTouchImageView.setDrawingCacheEnabled(true);
        AppResources.sCurrentImage = Bitmap.createBitmap(mTouchImageView.getDrawingCache());