setColorFilter在API29上已弃用 [英] setColorFilter is deprecated on API29
问题描述
我使用以下行更改VectorDrawable的颜色:
I use the following line to change the color of a VectorDrawable:
mydrawable.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
这很好用,尽管现在已弃用.该文档建议我使用:
This works nice, though it is now deprecated. The documentation suggests that I use:
mydrawable.getBackground().setColorFilter(new BlendModeColorFilter(color, PorterDuff.Mode.SRC_ATOP))
但是,BlendModeColorFilter
仅在API29上可用.在检查了不赞成使用的方法的源之后,我意识到它调用了:
Though, BlendModeColorFilter
is only available on API29. After examining the source of the deprecated method, I have realized that it calls:
new PorterDuffColorFilter()
所以,我继续使用:
mydrawable.getBackground().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP))
着色有效.这是不推荐使用的方法的正确替代方法,还是我必须在API29上使用BlendModeColorFilter?
The coloring worked. Is this the right replacement for the deprecated method or I must use BlendModeColorFilter on API29?
谢谢.
推荐答案
尝试一下:
public class MyDrawableCompat {
public static void setColorFilter(@NonNull Drawable drawable, @ColorInt int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
drawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_ATOP));
} else {
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
}
这:
MyDrawableCompat.setColorFilter(mydrawable.getBackground(), color);
这篇关于setColorFilter在API29上已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!