簡體   English   中英

設置(PNG)位圖顏色而不是逐像素設置的更快方法

[英]Faster way to set a (PNG) bitmap color instead of pixel by pixel

我有一些要應用顏色的png文件。 顏色根據用戶選擇而變化。 我通過其他方法設置的3個RGB值更改顏色。 png文件是隨機形狀,在形狀外部具有完全透明性。 我不想修改透明度,只能修改RGB值。 目前,我正在逐像素設置RGB值(請參見下面的代碼)。

我已經意識到這在應用程序中非常慢,而且效率可能不夠。 有沒有更好的方法可以做到這一點?

這是我目前正在做的事情。 您會看到,像素陣列對於占據屏幕相當一部分的圖像來說是巨大的:

public void foo(Component component, ComponentColor compColor, int userColor) {
    int h = component.getImages().getHeight();
    int w = component.getImages().getWidth();
    mBitmap = component.getImages().createScaledBitmap(component.getImages(), w, h, true);

    int[] pixels = new int[h * w];

    //Get all the pixels from the image
    mBitmap[index].getPixels(pixels, 0, w, 0, 0, w, h);

    //Modify the pixel array to the color the user selected
    pixels = changeColor(compColor, pixels);

    //Set the image to use the new pixel array
    mBitmap[index].setPixels(pixels, 0, w, 0, 0, w, h);
}

public int[] changeColor(ComponentColor compColor, int[] pixels) {
    int red = compColor.getRed();
    int green = compColor.getGreen();
    int blue = compColor.getBlue();
    int alpha;

    for (int i=0; i < pixels.length; i++) {
        alpha = Color.alpha(pixels[i]);
        if (alpha != 0) {
            pixels[i] = Color.argb(alpha, red, green, blue);
        }
    }
    return pixels;
}

您是否看過Bitmap中可用的功能? 諸如extractAlpha之類的聲音聽起來可能很有用。 您還將了解在Android中實現此類功能的方式,以了解如果它不能完全滿足您的需求,如何使它適應特定情況。

對我有用的答案是在這里寫Square, 透明的jpegs

他們提供了執行此操作的更快代碼段。 我嘗試了extractAlpha,但沒有成功,但Square的解決方案成功了。 只需修改其解決方案,即可修改顏色位而不是alpha位。

pixels[x] = (pixels[x] & 0xFF000000) | (color & 0x00FFFFFF);

暫無
暫無

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

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