5

Używam DrawableCompat.wrap, aby ustawić odcień na rysunkach w pre Lollipop i działa dobrze. DrawableCompat.unwrap nie działa przed Lollipop. Nie mogę uzyskać oryginalnego wyciągnięcia przed odcieniem.DrawableCompat.unwrap nie działa pre Lollipop

Na przykład:

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

W gotowych urządzeń lizak DrawableCompact.unwrap Zwraca rozciągliwej z odcieniem, a nie oryginalny

+0

Wszelkie wiadomości na ten temat? Znalazłeś rozwiązanie? – chrisonline

+0

@cherisonline Nic nowego. Nie używamy tej funkcjonalności z powodu tego zachowania, co jest niefortunne, ponieważ jest silnym narzędziem. – user1787773

Odpowiedz

1

Jeśli chcesz usunąć odcień, Call DrawableCompat.setTintList(drawable, null).

Unwrap nie jest funkcją destrukcyjną, wystarczy, że uzyskasz dostęp do oryginalnego wyciągu.

Poniżej znajduje się kod przykład:

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

W sprawa kod powinien być:

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); 
} 
Powiązane problemy