PNG lose transparency in Android (if all pixels are not transparent)

I have an application with two views - one on top of the other. At the top, I use a bitmap (ARGB_8888) downloaded from the PNG resource, and I play with its alpha channel so that some parts of it disappear and one of them becomes visible. Everything works fine if the original image has at least one transparent pixel to start with. But if the original PNG does not have transparent pixels, then changing its alpha to 0 makes the pixel i changed to black, not transparent.

Any ideas what can be done to fix this? sort of:

aaptOptions {
    cruncherEnabled = false
}

but another option?

I am currently modifying the source images before compilation, making the tiny area “translucent”, but would like to avoid this.

+4
source share
1 answer

Ok Finally understood.

I had to add one line. Instead:

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

Now I use:

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

and no need to add a transparent pixel to the original image!

+3
source

Source: https://habr.com/ru/post/1654199/


All Articles