I want to remove the white background color in a bitmap like in this example image.
This is my first try:
BitmapDrawable drawableImg = getImage();
drawableImg.setColorFilter(new PorterDuffColorFilter(Color.WHITE,
PorterDuff.Mode.DST_OUT));
Second try:
To remove the white colors range by setting fromBgColor and toBgColor, But when I replace the color with android transparent color the image turns into black here is the code:
public static Bitmap getBitmapWithTransparentBG(Bitmap srcBitmap,
int fromBgColor, int toBgColor) {
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 >= fromBgColor && nPixelColor <= toBgColor)
result.setPixel(x, y, Color.TRANSPARENT);
}
return result;
}
Hint I had a hint that bitmap does not support transparent bits and I should use PNG or Gif instead of bitmaps is that right?
Hint 2 It turned out that bitmap in Android has an option to show transparency since API level 1 using Bitmap.Config enum. Check this link in the documentation of Android.