1. 程式人生 > >程式碼動態改變SVG向量圖顏色

程式碼動態改變SVG向量圖顏色

    /**
     * 改變SVG圖片著色
     * @param imageView
     * @param iconResId svg資源id
     * @param color 期望的著色
     */
    public void changeSVGColor(ImageView imageView,int iconResId,int color){
        Drawable drawable =  AppCompatDrawableManager.get().getDrawable(mContext, iconResId);
        imageView.setImageDrawable(drawable);
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable drawable1 = DrawableCompat.wrap(state == null
? drawable : state.newDrawable()).mutate(); drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); DrawableCompat.setTint(drawable1, ContextCompat.getColor(mContext, color)); imageView.setImageDrawable(drawable1); }


注:
Drawable drawable = AppCompatDrawableManager.get().getDrawable(mContext, iconResId);

Drawable drawable = ContextCompat.getDrawable(mContext, iconResId);
較低api下,如紅米1s,報錯如下
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector