在Android中的画布上显示位图图像的一部分

时间:2011-12-15 21:01:51

标签: android canvas matrix bitmap drawbitmap

我正在尝试在我的Android应用中构建Magnify工具。为此,我有一个ImageView,我转换为Bitmap(有一些缩放/比例因子)。

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);

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

我尝试使用方法:“Matrix上的setRectToRect()”,“canvas.drawBitmap(位图,src,dst,paint)”。但是,没有适当的解决方法。

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

2 个答案:

答案 0 :(得分:3)

为什么不使用以下内容?

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

src对应于您要绘制的位图的哪个区域,dst表示您想要绘制位图的位置。您可以在此处详细了解:http://developer.android.com/reference/android/graphics/Canvas.html

答案 1 :(得分: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);