How to tint a Bitmap to a solid color How to tint a Bitmap to a solid color android android

How to tint a Bitmap to a solid color


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


Just to give a more complete answer.

This will take a bitmap and output a new tinted bitmap:

public static Bitmap tintImage(Bitmap bitmap, int color) {    Paint paint = new Paint();    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));    Bitmap bitmapResult = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);    Canvas canvas = new Canvas(bitmapResult);    canvas.drawBitmap(bitmap, 0, 0, paint);    return bitmapResult;}


If your bitmap is a drawable that you want to use in a layout, then you can make a new drawable (.xml) that references your original drawable (e.g .png).

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android"    android:src="@drawable/plus"     android:tint="#2c53e5" />