5

Ich verwende DrawableCompat.wrap, um Farbton auf Zeichen in Pre Lollipop einzustellen und es funktioniert gut. DrawableCompat.unwrap funktioniert nicht vor Lollipop. Ich kann das Original vor dem Farbton nicht zeichnen.DrawableCompat.unwrap funktioniert nicht vor Lollipop

Zum Beispiel:

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 prä Lutscher Geräte DrawableCompact.unwrap die ziehbar mit dem Farbton zurückkehrt und nicht das ursprüngliche

+0

alle Nachrichten über das? Hast du eine Lösung gefunden? – chrisonline

+0

@chrisonline Nichts neues. Wir verwenden diese Funktionalität wegen dieses Verhaltens nicht, was bedauerlich ist, weil es ein starkes Werkzeug ist. – user1787773

Antwort

1

Wenn Sie den Farbton löschen möchten, rufen Sie DrawableCompat.setTintList(drawable, null).

Unwrap ist keine destruktive Funktion, es ist nur für Sie da, um Zugriff auf die ursprüngliche Drawable zu bekommen.

Das Folgende ist ein Beispielcode:

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 Ihrem Fall sollte der Code sein:

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); 
}