欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图标(drawable)着色

程序员文章站 2024-02-10 23:49:22
...

1、

   /**
     * 为ImageView中的drawable着色
     *
     * @param view  ImageView控件
     * @param colorResId    着色的颜色id
     */
    public void setViewDrawableColor(ImageView view, int colorResId) {
        //获取view中的drawable并使其可变
        Drawable modeDrawable = view.getDrawable().mutate();
        //对modeDrawable进行包装,使其在不同版本中设置着色生效
        Drawable temp = DrawableCompat.wrap(modeDrawable);
        //通过colorResId获取颜色值
        ColorStateList colorStateList = ColorStateList.valueOf(view.getResources().getColor(colorResId));
        //着色
        DrawableCompat.setTintList(temp, colorStateList);
        view.setImageDrawable(temp);
    }

2、

   /**
     * 为单个drawable着色并返回着色后的drawable
     *
     * @param drawable
     * @param colorResId
     * @return
     */
    public Drawable setDrawableColor(Drawable drawable, int colorResId){
        Drawable modeDrawable = drawable.mutate();
        Drawable temp = DrawableCompat.wrap(modeDrawable);
        DrawableCompat.setTint(temp,colorResId);
        return temp;
    }

相关标签: drawable着色