Android的应用嘉洛斯colorFilter上的ImageView的一部分带着面具 [英] Android apply colorMatrix colorFilter on part of imageView with a mask
问题描述
我要改变亮度上的图像的某一部分。我知道如何使用嘉洛斯来改变图像的亮度(或色度)。但是,这将被应用到整个图像。
I want to change the brightness on certain part of an image. I know how to use ColorMatrix to change the brightness(or hue) of an image. But it will be applied to the whole image.
我有一个模板文件(黑白图像)。我想申请的亮度变化的仅上mask.How的白色部分做到这一点在Android的?
I have a mask file (black and white image). I want to apply the brightness change only on the the white part of that mask.How to do this in Android?
下面是我希望得到一个面具的形象和结果。
Below is a mask image and the result I want to get.
推荐答案
对于给定的位图和掩码:
for given bitmap and mask:
首先创建一个临时的位图:
first create a temporary bitmap:
bitmap = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.bitmap);
mask = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.mask);
float[] src = {
0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
0, 0, 0, 0, 255,
1, 1, 1, -1, 0,
};
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, 0, 0, null);
c.drawBitmap(mask, 0, 0, maskPaint);
colorFilterPaint = new Paint();
colorFilterPaint.setColorFilter(new LightingColorFilter(0xffffff, 0x880000));
和绘制它(我缩放它,因为我的模拟器缩小了):
and draw it (I scaled it up since my emulator scaled it down):
@Override
public void draw(Canvas canvas) {
canvas.save();
canvas.scale(3, 3);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawBitmap(filteredBitmap, 0, 0, colorFilterPaint);
canvas.restore();
}
和的结果是:
这篇关于Android的应用嘉洛斯colorFilter上的ImageView的一部分带着面具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!