2

我正在尝试在我的 Android 应用程序中构建一个放大工具。为此,我有一个 ImageView,我将其转换为位图(具有一些缩放/比例因子)。

imageView.setDrawingCacheEnabled(true);
Bitmap drawingCache = imageView.getDrawingCache(true);
Matrix matrix = new Matrix();
matrix.postScale(5, 5);        

Bitmap viewCapture = Bitmap.createBitmap(drawingCache, 0, 0, 
                                         drawingCache.getWidth(),
                                         drawingCache.getHeight(), 
                                         matrix, true);
imageView.setDrawingCacheEnabled(false);

现在,我将这个位图图像“viewCapture”绘制到我的画布上。在这里,我只想在画布上呈现图像的一部分。

我尝试使用方法:“矩阵上的 setRectToRect()”、“canvas.drawBitmap(bitmap, src, dst, paint)”。但是,没有适当地发挥作用。

使用 SurfaceViews 会有帮助吗?有没有人遇到过这种情况?请发表您的想法/想法。

4

2 回答 2

3

为什么不只使用以下内容?

 Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

src对应于您要绘制的位图区域,并dst说明您要绘制位图的位置。你可以在这里阅读更多关于它的信息:http: //developer.android.com/reference/android/graphics/Canvas.html

于 2011-12-15T21:17:00.387 回答
0

当我这样做时,我会得到一个较小的位图图像,而不是裁剪的图像。以下是我使用的片段。

            Canvas c = holder.lockCanvas();
            c.drawRGB(10, 10, 10);              
            Rect src = new Rect((int)x - _image.getWidth(), (int) y - _image.getHeight(), (int)x + _image.getWidth(), (int) y + _image.getHeight());
            RectF dst = new RectF((int)x - 50, (int) y - 50, (int)x + 50, (int) y + 50);
            c.drawBitmap(_image, src, dst, null);
            holder.unlockCanvasAndPost(c);
于 2011-12-16T16:56:00.683 回答