Skip to content

Instantly share code, notes, and snippets.

@icastell
Created July 14, 2014 06:44
Show Gist options
  • Save icastell/c3fe703abfd0722ab068 to your computer and use it in GitHub Desktop.
Save icastell/c3fe703abfd0722ab068 to your computer and use it in GitHub Desktop.
Runtime asset coloring in Android
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;
}
@icastell
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment