如何为drawable着色?

时间:2011-09-14 11:25:18

标签: android colors drawable hsl hsv

如何通过设置色调,饱和度增量和亮度增量来着色Android drawable?

1 个答案:

答案 0 :(得分:1)

尚未对此进行测试,但它应该有效。

public static BitmapDrawable colorize(BitmapDrawable d, float hue, float saturationDelta, float valueDelta) {
    Bitmap src = d.getBitmap();
    Bitmap b = src.copy(Bitmap.Config.ARGB_8888, true);
    for (int x = 0; x < b.getWidth(); x++) {
        for (int y = 0; y < b.getHeight(); y++) {
            int color = b.getPixel(x, y);
            float[] hsv = new float[3];
            Color.colorToHSV(color, hsv);
            hsv[0] = hue;
            hsv[1] += saturationDelta;
            hsv[2] += valueDelta;
            int newColor = Color.HSVToColor(Color.alpha(color), hsv);
            b.setPixel(x, y, newColor);
        }
    }
    return new BitmapDrawable(b);
}