How to change colors of a Drawable in Android?
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 different color, say blue, and then cache the resultant Drawable object so I can use it later.
So for example say I have a 20x20 PNG file that has a white circle in the middle, and that everything outside the circle is transparent. What's the best way to turn that white circle blue and cache the results? Does the answer change if I want to use that source image to create several new Drawables (say blue, red, green, orange, etc)?
I'm guessing that I'll want to use a ColorMatrix in some way, but I'm not sure how.
Solution 1:
I think you can actually just use Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )
. This would set white pixels to red but I don't think it would affect the transparent pixels.
See Drawable#setColorFilter
Solution 2:
Give this code a try:
ImageView lineColorCode = (ImageView)convertView.findViewById(R.id.line_color_code);
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);