Android 在带有遮罩的部分 imageView 上应用 colorMatrix colorFilter [英] Android apply colorMatrix colorFilter on part of imageView with a mask

查看:22
本文介绍了Android 在带有遮罩的部分 imageView 上应用 colorMatrix colorFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变图像某些部分的亮度.我知道如何使用 ColorMatrix 来改变图像的亮度(或色调).但它将应用于整个图像.

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.

我有一个遮罩文件(黑白图像).我想在该蒙版的白色部分应用亮度变化.如何在 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 在带有遮罩的部分 imageView 上应用 colorMatrix colorFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆