I have a png file that is transparent outside which is used as an icon image somewhere in my UI; I'd like for the user to be able to specify a different color for it at runtime (as in the image on the left below, assuming I have the android icon); or a different "background" image (Image on the right below, suppose the icon is a shape combining a circle and a triangle). I read this post which used ColorFilter to set all the white color pixels to another color in code; but it doesn't allow me to change the "background" of the icon. I guess I probably can do this with a ClipDrawable but I can't figure out how.

If you use ImageView, you can set also the source of image (android:src) but also background for the image (android:background). So if your image has a transparent region, or region with alpha channel, the background color will be used for these pixels.
The background color then can be changed dynamically. Can you use this approach?
imageView.setBackgroundColor(0x00FF0000); // red
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With