使用 Canvas 绘制位图的各个部分

发布于 2024-10-29 07:25:52 字数 433 浏览 1 评论 0原文

我有一张图片(资源),我想将其用于 Android 中的应用程序。但我只想画出其中的特定部分。我最初的想法是将其变成位图并指定需要绘制哪些像素以及在哪里。我尝试了 canvas.drawBitmap(bitmap, src, dst, null);但它似乎不起作用。也许我没有正确使用它。

只是想知道这是否可能,我可以用什么来实现这一目标?

谢谢!

src = new Rect(20,40,20,40);
dst = new Rect(20,40,20,40);

canvas.drawBitmap(background, offset, 0, null);
canvas.drawBitmap(bitmap, src, dst, null);

我希望看到 src 坐标指定的区域被绘制到 dst 坐标指定的区域中,但是除了背景之外我什么也没看到。

I have a picture (resource) that I would like to use for my application in Android. But I only want to draw specific segments of it. My initial thought is to turn it into a bitmap and specify which pixels need to be drawn and where. I tried canvas.drawBitmap(bitmap, src, dst, null); but it doesn't seem to work. Maybe I am not using it right.

Just wondering if it is possible at all, and what can I use to achieve this?

Thanks!

src = new Rect(20,40,20,40);
dst = new Rect(20,40,20,40);

canvas.drawBitmap(background, offset, 0, null);
canvas.drawBitmap(bitmap, src, dst, null);

I was hoping to see the area specified at src's coordinates to be drawn into the area specified by dst's coordinates, but I don't see anything, other than the background.

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

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

发布评论

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

评论(2

余生再见 2024-11-05 07:25:52

dst 应该是您想要在画布中绘制图像的位置,而 src 应该是您想要从中裁剪的矩形。

dst should be where you want to draw the image in the canvas, and the src should be the Rect you want crop from.

╭⌒浅淡时光〆 2024-11-05 07:25:52

您可能想要使用支持 Alpha 通道的格式或加载位图以及 Alpha 通道的灰度图像,从两者构建图像并绘制该图像。尝试 Java 的 Graphics2D 对象。 这里是一篇可以帮助您入门的文章。

You might want to use a format that supports an alpha channel or load the bitmap as well as a greyscale image for the alpha channel, construct an image from both and draw that. Try Java's Graphics2D object. Here's an article that should get you started.

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