setColorFilter is deprecated on API29

前端 未结 3 1848
眼角桃花
眼角桃花 2020-12-09 14:40

I use the following line to change the color of a VectorDrawable:

mydrawable.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP)

Thi

3条回答
  •  悲&欢浪女
    2020-12-09 14:55

    Try this:

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

    And this:

    MyDrawableCompat.setColorFilter(mydrawable.getBackground(), color);
    

提交回复
热议问题