开发者

Android: how apply transparency mask to a Bitmap?

开发者 https://www.devze.com 2023-03-30 06:23 出处:网络
I have a ARGB_8888 Bitmap, which I need to apply an alpha mask to prior to painting to the canvas.The Alp开发者_开发问答ha mask is a stream of bytes representing the alpha channel for the image.I coul

I have a ARGB_8888 Bitmap, which I need to apply an alpha mask to prior to painting to the canvas. The Alp开发者_开发问答ha 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?


First things first, make sure you have a mutable Bitmap with Bitmap.isMutable().

Have you tried Bitmap.setHasAlpha and using arithmetic operations on your mutable Bitmap 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

int desiredAlpha = 0x0F000000;
for(int i = 0; i < Bitmap.getwidth; i++)
{
    for(int j = 0; j < Bitmap.getHeight; j++)
    {
         Bitmap.setPixel(Bitmap.getPixel(i,j) + desiredAlpha);
    }
}

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消