使用 Canvas 绘制位图的各个部分
我有一张图片(资源),我想将其用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
您可能想要使用支持 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.