My goal is to make particular color transparent on image as in tiles to games where one plain background color is ignored while loadling.
What I've read
- Draw a Bitmap on a Canvas replacing Black Pixels to Transparent
- Replacing a color in a Bitmap
- How to change colors of a Drawable in Android?
- Android: Make specific (green) color in background image transparent
- Android how to apply mask on ImageView?
Code I have added to default .java
ImageView log = (ImageView) findViewById(R.id.iconLogo);
int col = ResourcesCompat.getColor(getResources(), R.color.colorTrans, null);
log.getDrawable().mutate().setColorFilter(col, PorterDuff.Mode.SRC_IN);
//log.getDrawable().setColorFilter(col, PorterDuff.Mode.SRC_IN); //take2
//log.setColorFilter(R.color.colorTrans, PorterDuff.Mode.SRC_IN); //take3
Making image mutable provides better result but still does not subtract R.color.colorTrans
from image. Overall after playing around with filter it seems to be working as a filter above whole image and there is no search for the color I set hence that color always present and not excluded not depending on mode used.
Second option
public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap, int BgColor) {
Bitmap result = srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
int nWidth = result.getWidth();
int nHeight = result.getHeight();
for (int y = 0; y < nHeight; ++y)
for (int x = 0; x < nWidth; ++x) {
int nPixelColor = result.getPixel(x, y);
if (nPixelColor == BgColor)
result.setPixel(x, y, Color.TRANSPARENT);
}
return result;
}
Using only one 600x600 px png file increases app loading time from 1-2s to 8-12s moreover it does not remove specific color. Hence can not be used for multiple igames.
What I need again is to remove/ignore specific color from Image in ImageView but not to make all pic transparent. Since games use tile maps without alpha then there must be a way to do it without adding alpha mask to them.