【问题标题】:How to remove white background color from BitmapDrawable or bitmap image in android?如何从 Android 中的 BitmapDrawable 或位图图像中删除白色背景颜色?
【发布时间】:2013-01-18 23:33:42
【问题描述】:

我想删除位图中的白色背景颜色,如本示例图像所示。

这是我的第一次尝试

BitmapDrawable drawableImg = getImage();
drawableImg.setColorFilter(new PorterDuffColorFilter(Color.WHITE,
PorterDuff.Mode.DST_OUT));

第二次尝试

通过设置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;
}

提示我提示位图不支持透明位,我应该使用 PNG 或 Gif 代替位图,对吗?

提示 2 事实证明,Android 中的位图具有显示透明度的选项,因为 API 级别 1 使用 Bitmap.Config 枚举。在 Android 的文档中查看这个link

【问题讨论】:

  • 嗨@khaled,你有没有找到任何解决方案。我正在寻找解决方案,需要将蒙版后的白色背景转换为透明。
  • 不。但现在我知道要做到这一点,我需要绘制 PNG 而不是位图,因为位图不支持透明背景。
  • 嘿@khaled,感谢您的回复。但我不理解您的观点,您将如何绘制 PNG 而不是位图。如果可能的话,你能指导我更多你将如何做到这一点。由于我长期以来一直被这个问题所困扰,并且没有找到任何解决方案。
  • 嘿@khaled,我也有同样的问题:stackoverflow.com/questions/18502946/…。你能帮我吗,是否可以从android中的图像中删除白色背景。

标签: android masking imagefilter


【解决方案1】:

你的图片下面是什么?也许这可能是一个布局问题。您是否尝试在 xml 中设置 android:background="@android:color/transparent" ? 我看到您尝试了中描述的 getBitmapWithTransparentBG Android: Make specific (green) color in background image transparent 但是你也试过这个吗? How to change colors of a Drawable in Android? 关于图像是 jpg 或 png,在加载并将其设置为 imageview(从中获取位图)后,我已经设法使用两者,并且它允许使用上面引用的 getBitmapWithTransparentBG 函数设置透明度。

【讨论】:

  • 在这个问题stackoverflow.com/questions/1309629/… 如果你想给它任何颜色但透明的,这个答案可以工作。由于位图没有透明度的概念。我不知道在最新的 SDK 中是否有可绘制的 png。但我需要 PNG 来支持透明颜色。我会检查并返回给你。
猜你喜欢
  • 1970-01-01
  • 2016-08-06
  • 2010-11-02
  • 2018-07-01
  • 2015-03-02
  • 1970-01-01
  • 2016-07-14
  • 2019-06-13
相关资源
最近更新 更多