0

我有以下代码可以将图像的对比度降低 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);

我在上面使用的数组给出了一个非常深的蓝色调。由于某种原因,似乎无法正确处理。

任何帮助将非常感激。非常感谢!

4

0 回答 0