How to tint a Bitmap to a solid color

Will Kru picture Will Kru · Jun 13, 2011 · Viewed 20k times · Source

How would one go about tinting a Bitmap to a solid color, effectively replacing all pixels that have an alpha > 0 to a given RGB value? In addition how to do the same thing, but keeping the alpha for every pixel? I'm not looking for per-pixel operations as they tend to be slow.

I tried using a ColorMatrixColorFilter and a ColorFilter, which do tint the Bitmap, but they colorize instead of performing a 100% tint.

Answer

Will Kru picture Will Kru · Jun 14, 2011

I solved this by using a PorterDuffColorFilter

Paint paint = new Paint();
paint.setColorFilter(new PorterDuffColorFilter(targetColor, PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(resource, matrix, paint);