我有以下代码可以将图像的对比度降低50%并将饱和度降低到0.完美无缺。但是,我需要在生成的图像上叠加一个颜色,就像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);
我上面使用的阵列给出了一种很暗的蓝色调。似乎无法出于某种原因做到正确。
非常感谢任何帮助。非常感谢!