android drawable颜色矩阵

时间:2012-01-24 09:29:59

标签: java android image

我有BitmapDrawable

我在屏幕上绘制

我有4个搜索栏(对比度,亮度,色调,饱和度)

我改变了BitmapDrawable的对比度:

public void setContrast(float mAngle) {
ColorMatrix matrix = new ColorMatrix();

    float contrast = mAngle / 180.f;

            float scale = contrast + 1.f;
    float translate = (-.5f * scale + .5f) * 255.f;

    float[] cntr = new float[] { scale, 0, 0, 0, translate, 0, scale, 0, 0, translate, 0, 0,
            scale, 0, translate, 0, 0, 0, 1, 0 };

            matrix.set(cntr);

    drawable.setColorFilter(new ColorMatrixColorFilter(matrix));
}

并改变BitmapDrawable的色调,如下所示:

public void setHue(float value {
     ColorMatrix matrix = new ColorMatrix();  

 value = cleanValue(value, 180f) / 180f * (float) Math.PI;
    if (value == 0) {
        return;
    }
    float cosVal = (float) Math.cos(value);
    float sinVal = (float) Math.sin(value);
    float lumR = 0.213f;
    float lumG = 0.715f;
    float lumB = 0.072f;
    float[] fmatrix = new float[] { lumR + cosVal * (1 - lumR) + sinVal * (-lumR),
            lumG + cosVal * (-lumG) + sinVal * (-lumG),
            lumB + cosVal * (-lumB) + sinVal * (1 - lumB), 0, 0,
            lumR + cosVal * (-lumR) + sinVal * (0.143f),
            lumG + cosVal * (1 - lumG) + sinVal * (0.140f),
            lumB + cosVal * (-lumB) + sinVal * (-0.283f), 0, 0,
            lumR + cosVal * (-lumR) + sinVal * (-(1 - lumR)),
            lumG + cosVal * (-lumG) + sinVal * (lumG),
            lumB + cosVal * (1 - lumB) + sinVal * (lumB), 0, 0, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f,
            0f, 1f };

         matrix.postConcat(new ColorMatrix(fmatrix));

     drawable.setColorFilter(new ColorMatrixColorFilter(matrix));
    }

     protected float cleanValue(float p_val, float p_limit) {
       return Math.min(p_limit, Math.max(-p_limit, p_val));
}

他们工作正常,但我想一起工作,有办法吗?

1 个答案:

答案 0 :(得分:0)

您需要连接颜色矩阵。查看ColorMatrix API中的setConcat函数。