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
Post a Comment