簡體   English   中英

Android位圖:將透明像素轉換為顏色

[英]Android Bitmap: Convert transparent pixels to a color

我有一個Android應用程序,可以將圖像作為位圖加載並在ImageView中顯示。 問題是圖像看起來具有透明背景; 這會導致圖像上的一些黑色文本在黑色背景下消​​失。

如果我將ImageView背景設置為白色,那種工作,但我在圖像上得到了丑陋的大邊框,它被拉伸以適合父級(實際圖像在中間縮放)。

所以 - 我想將位圖中的透明像素轉換為純色 - 但我無法弄清楚如何做到這一點!

任何幫助將不勝感激!

謝謝克里斯

如果您將圖像作為資源包含在內,最簡單的方法就是在像gimp這樣的程序中自己編輯圖像。 您可以在那里添加背景,並確保它看起來像什么,並且沒有用於處理每次加載時修改圖像的功率。

如果您自己無法控制圖像,可以通過執行類似的操作來修改它,假設您的Bitmap稱為image

Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig());  // Create another image the same size
imageWithBG.eraseColor(Color.WHITE);  // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG);  // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle();  // clear out old image 

您可以遍歷每個像素並檢查它是否透明。

像這樣的東西。 (未測試)

        Bitmap b = ...;
        for(int x = 0; x<b.getWidth(); x++){
            for(int y = 0; y<b.getHeight(); y++){
                if(b.getPixel(x, y) == Color.TRANSPARENT){
                    b.setPixel(x, y, Color.WHITE);
                }
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM