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

问题描述 投票:1回答:1

我想删除位图中的白色背景颜色,就像在此示例图像中一样。

这是我的第一次尝试:

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中的位图可以选择使用Bitmap.Config枚举来显示API级别1以来的透明度。在Android文档中查看此link

android masking imagefilter
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.