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

发布于 2024-12-21 05:56:04 字数 770 浏览 6 评论 0原文

我正在尝试在我的 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”绘制到我的画布上。在这里,我只想在画布上渲染图像的一部分。

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

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

I am trying to build a Magnify tool in my Android app. For this, I have an ImageView, which I converted to Bitmap (with some zoom/scale factor).

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

Now, I am drawing this Bitmap image "viewCapture" to my canvas. Here, I want only portion of the image to be rendered on the canvas.

I tried using approaches: "setRectToRect() on Matrix", "canvas.drawBitmap(bitmap, src, dst, paint)". But, didn't work out appropriately.

Would using SurfaceViews be helpful? Has anyone come across this situation? Please post your thoughts/ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-12-28 05:56:04

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

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

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

Why not just use the following?

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

src corresponds to which region of the bitmap you want to draw and dst says where you want the bitmap drawn. You can read more about it here: http://developer.android.com/reference/android/graphics/Canvas.html

情深缘浅 2024-12-28 05:56:04

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

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

When I do that, I get a smaller image of the bitmap, instead of cropped image. Following is the snippet I have used.

            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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文