所以我遇到了一個奇怪的問題......我製作了一些代碼來爲Drawable着色,並且它適用於Vector資產的所有Android版本,但不適用於常規PNG資產。代碼如下:可繪製的着色代碼適用於Vectors,但不適用於PNG
public class TintHelper {
private Context mContext;
public TintHelper(Context context) {
mContext = context;
}
public Drawable getTintedDrawableFromResource(int resourceID, ColorStateList colorStateList) {
Drawable original = AppCompatDrawableManager.get().getDrawable(mContext, resourceID);
return performTintOnDrawable(original, colorStateList);
}
private Drawable performTintOnDrawable(Drawable drawable, ColorStateList colorStateList) {
Drawable tinted = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(tinted, colorStateList);
return tinted;
}
}
當我指定一個矢量資源的資源ID,代碼完美的作品,按下時圖像着色,但是當我使用一個普通PNG,沒有應用色調時,圖標被按下。如果任何人有任何想法,爲什麼這不起作用,請發佈一種可能支持這兩種資產類型的替代方法。
提前致謝!
你正在使用什麼版本的'appcompat-v7' /'support-v4'?最新的? 24.2.0? – pskink
@pskink 24.2.1。請參閱下面的答案以獲取解決方案。 – privatestaticint
它只適用於24.2.0,我(雙)檢查,不需要自定義視圖 – pskink