Search code examples
androidcolorstint

How to tint a Bitmap to a solid color


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.


Solution

  • 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);