Android:加载alpha蒙版位图

时间:2010-10-28 19:12:00

标签: android graphics

我有一个单通道PNG文件我想用作Porter-Duff绘图操作的alpha蒙版。如果我在没有任何选项的情况下加载它,则生成的Bitmap具有RGB_565配置,即被视为灰度。如果我将首选配置设置为ALPHA_8,则会将其作为灰度ARGB_8888加载。

如何说服Android将此文件视为alpha蒙版而不是灰度图像?

mask1 = BitmapFactory.decodeStream(pngStream);
// mask1.getConfig() is now RGB_565

BitmapFactory.Options maskOpts = new BitmapFactory.Options();
maskOpts.inPreferredConfig = Bitmap.Config.ALPHA_8;
mask2 = BitmapFactory.decodeStream(pngStream, null, maskOpts);
// mask2.getConfig() is now ARGB_8888 (the alpha channel is fully opaque)

1 个答案:

答案 0 :(得分:7)

更多的解决方法而非解决方案:

我现在将alpha通道包含在RGBA PNG文件中,RGB通道全为零。我可以使用ARGB_8888的首选配置加载此文件,然后提取其alpha通道。这会在掩码文件中浪费几KB,并在解码图像时浪费大量内存。

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeStream(pngStream, null, opts);
Bitmap mask = source.extractAlpha();
source.recycle();
// mask.getConfig() is now ALPHA_8
相关问题