setColorFilter在API29上已弃用 [英] setColorFilter is deprecated on API29

查看:790
本文介绍了setColorFilter在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆