title: android studio | setting specific color to transparent on image
body: <p>My goal is to make particular color transparent on image as in tiles to games where one plain background color is ignored while loadling.</p>
  
  <p>What I've read </p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/14895015/draw-a-bitmap-on-a-canvas-replacing-black-pixels-to-transparent">Draw a Bitmap on a Canvas replacing Black Pixels to Transparent</a></li>
  <li><a href="https://stackoverflow.com/questions/18748694/replacing-a-color-in-a-bitmap">Replacing a color in a Bitmap</a></li>
  <li><a href="https://stackoverflow.com/questions/1309629/how-to-change-colors-of-a-drawable-in-android">How to change colors of a Drawable in Android?</a></li>
  <li><a href="https://stackoverflow.com/questions/5155999/android-make-specific-green-color-in-background-image-transparent">Android: Make specific (green) color in background image transparent</a></li>
  <li><a href="https://stackoverflow.com/questions/14801075/android-how-to-apply-mask-on-imageview?noredirect=1&amp;lq=1">Android how to apply mask on ImageView?</a></li>
  </ol>
  
  <p>Code I have added to default .java </p>
  
  <pre><code>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
  </code></pre>
  
  <p>Making image mutable provides better result but still does not subtract <code>R.color.colorTrans</code> 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.</p>
  
  <p>Second option</p>
  
  <pre><code> 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 &lt; nHeight; ++y)
              for (int x = 0; x &lt; nWidth; ++x) {
                  int nPixelColor = result.getPixel(x, y);
                  if (nPixelColor == BgColor)
                      result.setPixel(x, y, Color.TRANSPARENT);
              }
          return result;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>