Android:如何将透明度蒙版应用于位图?
我有一个 ARGB_8888 位图,在绘制到画布之前我需要对其应用 alpha 遮罩。 Alpha 掩码是表示图像 Alpha 通道的字节流。我可以检索位图像素值的 int 数组,循环遍历数组并为每个像素设置 alpha,从新像素值创建位图,并将其绘制到画布上;但这似乎效率极低。因此,我希望使用一些内置功能将蒙版应用于位图;但找不到任何有用的东西。有什么建议吗?
I have a ARGB_8888 Bitmap, which I need to apply an alpha mask to prior to painting to the canvas. The Alpha mask is a stream of bytes representing the alpha channel for the image. I could retrieve the int array of pixel values for the Bitmap, loop through the array and set the alpha for each pixel, create a bitmap from the new pixel values, and paint it to the canvas; but this seems extremely inefficient. So, I was hoping to use some built in functionality for apply masks to bitmaps; but can't find anything useful. Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,确保您有一个带有
Bitmap.isMutable()
的可变位图。您是否尝试过
Bitmap.setHasAlpha
并在可变Bitmap
上使用算术运算来简单地“添加”Alpha 通道?也就是说,如果您的像素为
#00FF0000
,则可以添加#FF000000
以使其完全透明。这比复制到新的位图要高效得多。
也许类似
希望这有帮助!
First things first, make sure you have a mutable Bitmap with
Bitmap.isMutable()
.Have you tried
Bitmap.setHasAlpha
and using arithmetic operations on your mutableBitmap
to simply "add" the alpha channel?That is, if you have a pixel that is
#00FF0000
, you can add#FF000000
to make it completely transparent.This would be much more efficient than copying to a new
Bitmap
.Maybe something like
Hope this helps!