How to change colors of a Drawable in Android?

前端 未结 21 1857
野性不改
野性不改 2020-11-22 13:50

I\'m working on an android application, and I have a drawable that I\'m loading up from a source image. On this image, I\'d like to convert all of the white pixels to a dif

21条回答
  •  北海茫月
    2020-11-22 14:14

    If you have a drawable that's a solid color and you want to change it to a differnet solid color, you can use a ColorMatrixColorFilter. Transparency is preserved.

    int iColor = Color.parseColor(color);
    
    int red   = (iColor & 0xFF0000) / 0xFFFF;
    int green = (iColor & 0xFF00) / 0xFF;
    int blue  = iColor & 0xFF;
    
    float[] matrix = { 0, 0, 0, 0, red,
                       0, 0, 0, 0, green,
                       0, 0, 0, 0, blue,
                       0, 0, 0, 1, 0 };
    
    ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
    drawable.setColorFilter(colorFilter);
    

提交回复
热议问题