安卓(Android) 怎么着色位图(Bitmap)资源(Resource)

2019年10月19日12:40:56安卓(Android) 怎么着色位图(Bitmap)资源(Resource)已关闭评论 318 1196字阅读3分59秒

1.简介

安卓(Android) 怎么着色位图(Bitmap)资源(Resource)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

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

 
继续阅读
编程十万个怎么办