繁体   English   中英

如何从Android中的BitmapDrawable或位图图像中删除白色背景色?

[英]How to remove white background color from BitmapDrawable or bitmap image in android?

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

这是我的第一次尝试

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事实证明,自API级别1使用Bitmap.Config枚举以来,Android中的位图具有显示透明度的选项。 检查Android文档中的此链接

在此处输入图片说明

您的形象之下是什么? 可能是布局问题。 您是否尝试过在xml中设置android:background =“ @ android:color / transparent”? 我看到您尝试了Android中描述的getBitmapWithTransparentBG :使背景图像中的特定(绿色)颜色透明,但是您是否也尝试过呢? 如何在Android中更改Drawable的颜色? 关于图像是jpg还是png,在加载并将其设置为imageview之后(从其返回位图),我设法使用了两者,并且它允许使用上述的getBitmapWithTransparentBG函数设置透明度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM