android - DrawableCompat.unwrap is not working pre Lollipop -


i'm using drawablecompat.wrap set tint on drawables in pre lollipop , it's working fine. drawablecompat.unwrap not working pre lollipop. can't original drawable before tint.

for example:

 if (v.isselected()){                 drawable normaldrawable = getresources().getdrawable(r.drawable.sample);                 drawable wrapdrawable = drawablecompat.wrap(normaldrawable);                 drawablecompat.settint(wrapdrawable, getresources().getcolor(r.color.sample_color));                 imagebutton.setimagedrawable(wrapdrawable);  }else{                 drawable normaldrawable = imagebutton.getdrawable();                 drawable unwrapdrawable = drawablecompat.unwrap(normaldrawable);                 imagebutton.setimagedrawable(unwrapdrawable);  } 

in pre lollipop devices drawablecompact.unwrap returns drawable tint , not original one

if want clear tint, call drawablecompat.settintlist(drawable, null).

unwrap isn't destructive function, it's there access original drawable.

the following example code:

drawable drawable = (drawable) contextcompat.getdrawable(getcontext(), r.drawable.google_image); if (condition) {     drawable = drawablecompat.wrap(drawable);     drawablecompat.settint(drawable, contextcompat.getcolor(getcontext(), r.color.grey700));     drawablecompat.settintmode(drawable, porterduff.mode.screen);     mimageview.setimagedrawable(drawable); } else {     drawable = drawablecompat.unwrap(drawable);     drawablecompat.settintlist(drawable, null);     mloginstatusgoogleimageview.setimagedrawable(drawable); } 

in your case code should be:

if (v.isselected()) {     drawable normaldrawable = getresources().getdrawable(r.drawable.sample);     drawable wrapdrawable = drawablecompat.wrap(normaldrawable);     drawablecompat.settint(wrapdrawable, contextcompat.getcolor(getcontext(), r.color.sample_color));     drawablecompat.settint(wrapdrawable, getresources().getcolor(r.color.sample_color));     imagebutton.setimagedrawable(wrapdrawable); } else {     drawable normaldrawable = imagebutton.getdrawable();     drawable unwrapdrawable = drawablecompat.unwrap(normaldrawable);     drawablecompat.settintlist(unwrapdrawable, null);     imagebutton.setimagedrawable(unwrapdrawable); } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -