簡體   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