Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:set gray scale filter to imageView

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

like image 330
mahdi Avatar asked Feb 03 '15 20:02

mahdi


People also ask

How to grayscale an image in Android studio?

To set all the views to grayscale, for TextView we have to use setTextColor to set a new color. For ImageView we can set a new resource which in itself is grayscale or use setColorFilter .

How do I create a gray scale image?

Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.


1 Answers

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); } 
like image 90
Paul LeBeau Avatar answered Sep 19 '22 17:09

Paul LeBeau