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



我正在尝试在我的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()上的矩阵","画布。"drawBitmap(bitmap, src, dst, paint)"。但是,没有适当地解决。

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

为什么不直接使用以下内容呢?

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

src对应于想要绘制位图的哪个区域,dst表示想要绘制位图的位置。您可以在这里阅读更多信息:http://developer.android.com/reference/android/graphics/Canvas.html

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

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

最新更新