Skip to content Skip to sidebar Skip to footer

Android: Loading An Alpha Mask Bitmap

I have a single-channel PNG file I'd like to use as an alpha mask for Porter-Duff drawing operations. If I load it without any options, the resulting Bitmap has an RGB_565 config,

Solution 1:

More of a workaround than a solution:

I'm now including the alpha channel in an RGBA PNG file with the RGB channels all zeroes. I can load this file with a preferred config of ARGB_8888 and then extract its alpha channel. This wastes a few KB in the mask file, and a lot of memory while decoding the image.

BitmapFactory.Optionsopts=newBitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmapsource= BitmapFactory.decodeStream(pngStream, null, opts);
Bitmapmask= source.extractAlpha();
source.recycle();
// mask.getConfig() is now ALPHA_8

Post a Comment for "Android: Loading An Alpha Mask Bitmap"