Eclipse中 VectorDrawable 动态改path颜色(Android5.1)

将SVG转为vectordrawable

常见的vectordrawable形式如下:



    
        
        
    


AndroidStudio 可以使用第三方库,可以按名字提取出path,单独来改变属性:VectorChildFinder
还可以使用style来动态改变颜色,但是都需要在style里预设:
https://stackoverflow.com/questions/33126904/change-fillcolor-from-vector-in-android-programmatically/38418049

在eclipse中目前我只用style方式成功过:
1.在style中声明属性:


        
        

2.在style中预设样式:




颜色自己定义即可
3.更改VectorDrawable:



    
        
        
    

这里面path的fillColor就是用的我们之前声明的属性
4.代码中动态改变:
代码中的控件:svg_iv(ImageView)
初始化的颜色:

ContextThemeWrapper wrapper = new ContextThemeWrapper(mContext, R.style.DefaultScene);
VectorDrawable mVectorDrawable = (VectorDrawable) getDrawable(R.drawable.ic_battery_full);
VectorDrawable newVectorDrawable = (VectorDrawable) mVectorDrawable.mutate();//使用此方法之后才能应用theme
Log.i(TAG, "can apply1:" + newVectorDrawable.canApplyTheme());
if(newVectorDrawable.canApplyTheme())// 判断能否应用theme
        newVectorDrawable.applyTheme(wrapper.getTheme());
svg_iv.setImageDrawable(new BatteryDrawable(mContext));

动态改变颜色:

ContextThemeWrapper wrapper = new ContextThemeWrapper(mContext, R.style.UpdatedScene);
VectorDrawable mVectorDrawable = (VectorDrawable) mContext.getDrawable(R.drawable.ic_battery_full);
VectorDrawable newVectorDrawable = (VectorDrawable) mVectorDrawable.mutate();
Log.i(TAG, "can apply:" + newVectorDrawable.canApplyTheme());
if(newVectorDrawable.canApplyTheme())
        newVectorDrawable.applyTheme(wrapper.getTheme());
svg_iv.setImageDrawable(new BatteryDrawable(mContext));

你可能感兴趣的:(Eclipse中 VectorDrawable 动态改path颜色(Android5.1))