对于给定的位图和掩码:
首先创建一个临时位图:
bitmap = BitmapFactory.decodeResource(ctx.getResources(),R.drawable.bitmap);
mask = BitmapFactory.decodeResource(ctx.getResources(),R.drawable.mask);
float[] src = {
0,255,1,-1,};
ColorMatrix cm = new ColorMatrix(src);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
maskPaint = new Paint();
maskPaint.setColorFilter(filter);
maskPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
filteredBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Config.ARGB_8888);
Canvas c = new Canvas(filteredBitmap);
c.drawBitmap(bitmap,null);
c.drawBitmap(mask,maskPaint);
colorFilterPaint = new Paint();
colorFilterPaint.setColorFilter(new LightingColorFilter(0xffffff,0x880000));
并绘制它(我的模拟器将其缩小后我将其缩放):
@Override
public void draw(Canvas canvas) {
canvas.save();
canvas.scale(3,3);
canvas.drawBitmap(bitmap,null);
canvas.drawBitmap(filteredBitmap,colorFilterPaint);
canvas.restore();
}
结果是: