colorize Bitmap Resource - Android App

Android examples for App:Resource

Description

colorize Bitmap Resource

Demo Code


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.v4.util.ArrayMap;

public class Main{
    private static final String TAG = ColorUtils.class.getSimpleName();
    public static Bitmap colorizeBitmapResource(Context context,
            int markerColor, int bitmapResId) {
        if (context == null) {
            return null;
        }/*from   w  w  w.  ja  va  2s .c  o  m*/
        return colorizeBitmap(markerColor, BitmapFactory.decodeResource(
                context.getResources(), bitmapResId));
    }
    public static Bitmap colorizeBitmap(int markerColor, Bitmap bitmap) {
        try {
            Bitmap obm = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), bitmap.getConfig());
            Canvas canvas = new Canvas(obm);
            canvas.drawBitmap(bitmap, 0f, 0f,
                    getNewPaintColorFilter(markerColor));
            return obm;
        } catch (Exception e) {
            MTLog.w(TAG, e, "Error while colorizing bitmap!");
            return bitmap;
        }
    }
    public static Paint getNewPaintColorFilter(int colorInt) {
        Paint paint = new Paint();
        paint.setColorFilter(new PorterDuffColorFilter(colorInt,
                PorterDuff.Mode.MULTIPLY));
        return paint;
    }
}

Related Tutorials