Created
July 14, 2014 06:44
-
-
Save icastell/c3fe703abfd0722ab068 to your computer and use it in GitHub Desktop.
Runtime asset coloring in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Drawable getColoredDrawable(Context context, int whiteDrawableResId, int targetColor) { | |
Drawable drawable = context.getResources().getDrawable(whiteDrawableResId); | |
ColorFilter filter = new LightingColorFilter(targetColor, 0); | |
drawable.mutate().setColorFilter(filter); | |
return drawable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The white drawable is colored with a LightingColorFilter. As documentation states,LightingColorFilter "multiplies the RGB channels by one color, and then adds a second color". If the source drawable is pure white, multiplying by the target color and adding zero will give you a drawable with the exact color you passed!