[英]How to remove white background color from BitmapDrawable or bitmap image in android?
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: 要通过设置fromBgColor和toBgColor删除白色范围,但是当我用android透明色替换颜色时,图像变成黑色,这是代码:
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? 提示我暗示位图不支持透明位,应该使用PNG或Gif代替位图,对吗?
Hint 2 It turned out that bitmap in Android has an option to show transparency since API level 1 using Bitmap.Config enum. 提示2事实证明,自API级别1使用Bitmap.Config枚举以来,Android中的位图具有显示透明度的选项。 Check this link in the documentation of Android.
检查Android文档中的此链接 。
what is beneath your image? 您的形象之下是什么? Maybe it could be a layout issue.
可能是布局问题。 Have you tried setting android:background="@android:color/transparent" in the xml?
您是否尝试过在xml中设置android:background =“ @ android:color / transparent”? I see you tried the getBitmapWithTransparentBG described in Android: Make specific (green) color in background image transparent But have you tried also this?
我看到您尝试了Android中描述的getBitmapWithTransparentBG :使背景图像中的特定(绿色)颜色透明,但是您是否也尝试过呢? How to change colors of a Drawable in Android?
如何在Android中更改Drawable的颜色? About the image being jpg or png, I've managed to work with both after loading and setting it to a imageview (getting the bitmap back from it), and it allows to set the transparency using the getBitmapWithTransparentBG function as quoted above.
关于图像是jpg还是png,在加载并将其设置为imageview之后(从其返回位图),我设法使用了两者,并且它允许使用上述的getBitmapWithTransparentBG函数设置透明度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.