28

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

mahdi
  • 16,257
  • 15
  • 52
  • 73

1 Answers1

76

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);
}
Alan
  • 1,510
  • 1
  • 18
  • 35
Paul LeBeau
  • 97,474
  • 9
  • 154
  • 181
  • 1
    Ah. I feared that might be the case. You could use the replace-color feature of svg-android (`getSVGFromInputStream(is, searchColor, replaceColor)`), or you may just have to render the SVG to a Bitmap and set the ImageView to use that. – Paul LeBeau Feb 04 '15 at 07:37
  • 1
    @PaulLeBeau you can use `clearColorFilter()` method which internally does the same thing which you have stated in soloution – Akshay Mukadam Mar 22 '19 at 04:32
  • @AkshayMukadam, yes 'clearColorFilter()' works well, but it doesn't give back the exact old color what was before the filter. Thanks. – Kotdroid Apr 20 '20 at 09:10
  • @Kotdroid then what is the alternate way can you share .Please – Akshay Mukadam Apr 21 '20 at 05:33
  • @AkshayMukadam , I don't have any alternative yet. But I used Paul's way. And it is working well. – Kotdroid Apr 21 '20 at 16:37