1
votes

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

  1. Draw a Bitmap on a Canvas replacing Black Pixels to Transparent
  2. Replacing a color in a Bitmap
  3. How to change colors of a Drawable in Android?
  4. Android: Make specific (green) color in background image transparent
  5. 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.

1
spriters-resource.com/resources/sheets/20/21264.png here is example of what I have meant. Orange color is ignoredJayJayAbrams

1 Answers

0
votes

Solution here unable to transparent pixels of a bitmap image .

However using it even on one picture increases loading time drastically. Probably .png with already cleared background would be better to use.