Skip to content Skip to sidebar Skip to footer

Android: Make Specific (green) Color In Background Image Transparent

i am writing an app for Android. in an xml file defining layout I have a TabHost with 6 Tabs, which all have the same big background image 'settingsdlg.gif'.

Solution 1:

You would have to change every green pixel into a transparent one. Here is an example: How to change colors of a Drawable in Android?

However if there are green pixels in the middle of the image then you can have a problem. So the other way is, if your image has constant size and shape, to create a mask and use xfer modes to create a new image with transparent rounded corners.

Solution 2:

Just if anyone had the same problem, here is the code:

//remove green edges from bg imageBitmapbgBitmap= ((BitmapDrawable)getResources().getDrawable(R.drawable.settingsdlg)).getBitmap();
    BitmaptransparentBgBitmap= Utils.getBitmapWithTransparentBG(bgBitmap, Color.GREEN);
    tabHost.setBackgroundDrawable(newBitmapDrawable(transparentBgBitmap));

in Utils:

publicstatic Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap, int bgColor) {
    Bitmapresult= srcBitmap.copy(Bitmap.Config.ARGB_8888, true);
    intnWidth= result.getWidth();
    intnHeight= result.getHeight();
    for (inty=0; y < nHeight; ++y)
      for (intx=0; x < nWidth; ++x) {
    intnPixelColor= result.getPixel(x, y);
    if (nPixelColor == bgColor)
      result.setPixel(x, y, Color.TRANSPARENT);
      }
    return result;
}

Solution 3:

This code snippet worked for me:

PorterDuffColorFilterporterDuffColorFilter=newPorterDuffColorFilter(
    getResources().getColor(R.color.your_color),
    PorterDuff.Mode.MULTIPLY
);
imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);

Post a Comment for "Android: Make Specific (green) Color In Background Image Transparent"