android:set gray scale filter to imageView

mahdi  picture mahdi · Feb 3, 2015 · Viewed 13.2k times · Source

i use this library to show svg image on imageView , i want to show locked and unlocked mode with this imageView, when state is locked show imageView in grayscale color filter. something like this code on css :

-webkit-filter: grayscale(100%); opacity: 0.5;

how can i do this ? can anybody help me ? thanks

Answer

Paul LeBeau picture Paul LeBeau · Feb 4, 2015

You should be able to make the image greyscale and faded using the following:

public static void  setLocked(ImageView v)
{
    ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0);  //0 means grayscale
    ColorMatrixColorFilter cf = new ColorMatrixColorFilter(matrix);
    v.setColorFilter(cf);
    v.setImageAlpha(128);   // 128 = 0.5
}

And reset it using:

public static void  setUnlocked(ImageView v)
{
    v.setColorFilter(null);
    v.setImageAlpha(255);
}