Piotr Piotr - 2 months ago 10
Android Question

PNGs losing transparency in Android (if all its pixels are not transparent)

I have an app with two views - one on top of another. On the top one I use a Bitmap (ARGB_8888) loaded from a PNG resource and I play with its alpha channel to make some parts of it disappear so the one below becomes visible. All works fine if the source image has at least a single transparent pixel to start with. But if the source PNG has no transparent pixels then changing it alpha to 0 makes the pixel I changed black, not transparent.

Any ideas what could be done to fix it? anything like:

aaptOptions {
cruncherEnabled = false
}


but another option?

Currently I modify the source images before compiling by making a tiny area of it "semi-transparent" but would like to avoid that.

Answer

Ok. Finally got it.

I had to add one line. Instead of:

mBitmap = BitmapFactory.decodeResource(getResources(), getResourceID()).copy(Bitmap.Config.ARGB_8888, true);

I am now using:

mBitmap = BitmapFactory.decodeResource(getResources(), getResourceID()).copy(Bitmap.Config.ARGB_8888, true);
mBitmap.setHasAlpha(true);

and there is no need to add a transparent pixel on the source image!

Comments