Android 在带有遮罩的部分 imageView 上应用 colorMatrix colorFilter

Android apply colorMatrix colorFilter on part of imageView with a mask(Android 在带有遮罩的部分 imageView 上应用 colorMatrix colorFilter)
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)