Java 使用ColorMatrix模拟PorterDuff覆盖模式覆盖纯色
我有下面的代码,它将图像的对比度降低50%,并将饱和度降低到0。工作完美无瑕。但是,我需要在生成的图像上覆盖一个给定的颜色,就像Java 使用ColorMatrix模拟PorterDuff覆盖模式覆盖纯色,java,android,image-processing,colormatrix,colormatrixfilter,Java,Android,Image Processing,Colormatrix,Colormatrixfilter,我有下面的代码,它将图像的对比度降低50%,并将饱和度降低到0。工作完美无瑕。但是,我需要在生成的图像上覆盖一个给定的颜色,就像PorterDuff.Mode.overlay过滤器一样 我已经摆弄这个好几个小时了,没有得到我想要的东西 float contrast = -0.5f; float scale = contrast + 1.f; float translate = (-.5f * scale + .5f) * 255.f;
PorterDuff.Mode.overlay
过滤器一样
我已经摆弄这个好几个小时了,没有得到我想要的东西
float contrast = -0.5f;
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
float[] array = new float[]{
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0};
ColorMatrix contrastMatrix = new ColorMatrix(array);
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(0);
ColorMatrix matrix = new ColorMatrix();
matrix.setConcat(contrastMatrix, saturationMatrix);
// This is where I've been trying to overlay the colour by fiddling the colour matrix array.
// Didn't get the effect I wanted.
int primaryColor = context.getResources().getColor(R.color.primary500);
matrix.postConcat(new ColorMatrix(
new float[]{
Color.red(primaryColor)/255f, 0, 0, 0, 0,
0, Color.green(primaryColor)/255f, 0, 0, 0,
0, 0, Color.blue(primaryColor)/255f, 0, 0,
0, 0, 0, 1, 0}));
imageView.setColorFilter(filter);
我在上面使用的阵列提供了一个非常深的蓝色色调。由于某种原因,似乎不能正确地使用它
任何帮助都将不胜感激。非常感谢 请参阅答案,了解深褐色色调。您可以更改不同颜色色调的值。@DerGolem是的,这样颜色叠加可以工作,但现在无法将对比度降低到50%。。。有什么想法吗?组合颜色矩阵,如示例所示:
matrixA.setConcat(matrixB,matrixA)代码>。顺便说一下,您的代码已经使用了这个技巧。只需添加另一个矩阵并将其连接到先前连接的2个矩阵,就可以得到3个矩阵的浓缩。