How to change colors of a Drawable in Android?

前端 未结 21 2151
野性不改
野性不改 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:08

    Int color = Color.GRAY; 
    // or int color = Color.argb(123,255,0,5);
    // or int color = 0xaaff000;
    

    in XML /res/values/color.xml

    
    
        #ff0000
     
    

    Java Code

    int color = ContextCompat.getColor(context, R.color.colorRed);
    
    GradientDrawable drawableBg = yourView.getBackground().mutate();
    drawableBg.setColor(color);
    

提交回复
热议问题