图标(drawable)着色

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

猜你喜欢

转载自blog.csdn.net/lllx9464/article/details/79393748