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

将SVG转为vectordrawable

常见的vectordrawable形式如下:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="64dp"
    android:viewportHeight="1024.0"
    android:viewportWidth="1024.0"
    android:width="64dp" >

    <group
        android:name="battery"
        android:pivotX="512"
        android:pivotY="512"
        android:rotation="90.0" >
        <path
            android:name="level"
            android:fillColor="#757575"
            android:pathData="M716.8,166.4h-89.6v-25.6c0,-14.1 -11.5,-25.6 -25.6,-25.6h-102.4c-14.1,0 -25.6,11.5 -25.6,25.6v25.6h-89.6c-14.1,0 -25.6,11.5 -25.6,25.6v691.2c0,14.1 11.5,25.6 25.6,25.6h332.8c14.1,0 25.6,-11.5 25.6,-25.6V192c0,-14.1 -11.5,-25.6 -25.6,-25.6z" />
        <path
            android:name="border"
            android:fillColor="#ffffff"
            android:pathData="M716.8,921.6H384c-21.8,0 -38.4,-16.6 -38.4,-38.4V192c0,-21.8 16.6,-38.4 38.4,-38.4h76.8v-12.8c0,-21.8 16.6,-38.4 38.4,-38.4h102.4c21.8,0 38.4,16.6 38.4,38.4v12.8h76.8c21.8,0 38.4,16.6 38.4,38.4v691.2c0,21.8 -16.6,38.4 -38.4,38.4zM384,179.2c-7.7,0 -12.8,5.1 -12.8,12.8v691.2c0,7.7 5.1,12.8 12.8,12.8h332.8c7.7,0 12.8,-5.1 12.8,-12.8V192c0,-7.7 -5.1,-12.8 -12.8,-12.8h-89.6c-7.7,0 -12.8,-5.1 -12.8,-12.8v-25.6c0,-7.7 -5.1,-12.8 -12.8,-12.8h-102.4c-7.7,0 -12.8,5.1 -12.8,12.8v25.6c0,7.7 -5.1,12.8 -12.8,12.8h-89.6z" />
    </group>

</vector>

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

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

<declare-styleable name="ChristmasTree">
        <attr name="border" format="color" />
        <attr name="inner" format="color" />
</declare-styleable>

2.在style中预设样式:

<style name="UpdatedScene" parent="DefaultScene">
        <item name="border">@color/battery_border</item>
        <item name="inner">@color/battery_low</item>
</style>

<style name="DefaultScene">
        <item name="border">@color/battery_border</item>
        <item name="inner">@color/battery_charging</item>
</style>

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

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="64dp"
    android:viewportHeight="1024.0"
    android:viewportWidth="1024.0"
    android:width="64dp" >

    <group
        android:name="battery"
        android:pivotX="512"
        android:pivotY="512"
        android:rotation="90.0" >
        <path
            android:name="level"
            android:fillColor="?attr/inner"
            android:pathData="M716.8,166.4h-89.6v-25.6c0,-14.1 -11.5,-25.6 -25.6,-25.6h-102.4c-14.1,0 -25.6,11.5 -25.6,25.6v25.6h-89.6c-14.1,0 -25.6,11.5 -25.6,25.6v691.2c0,14.1 11.5,25.6 25.6,25.6h332.8c14.1,0 25.6,-11.5 25.6,-25.6V192c0,-14.1 -11.5,-25.6 -25.6,-25.6z" />
        <path
            android:name="border"
            android:fillColor="?attr/border"
            android:pathData="M716.8,921.6H384c-21.8,0 -38.4,-16.6 -38.4,-38.4V192c0,-21.8 16.6,-38.4 38.4,-38.4h76.8v-12.8c0,-21.8 16.6,-38.4 38.4,-38.4h102.4c21.8,0 38.4,16.6 38.4,38.4v12.8h76.8c21.8,0 38.4,16.6 38.4,38.4v691.2c0,21.8 -16.6,38.4 -38.4,38.4zM384,179.2c-7.7,0 -12.8,5.1 -12.8,12.8v691.2c0,7.7 5.1,12.8 12.8,12.8h332.8c7.7,0 12.8,-5.1 12.8,-12.8V192c0,-7.7 -5.1,-12.8 -12.8,-12.8h-89.6c-7.7,0 -12.8,-5.1 -12.8,-12.8v-25.6c0,-7.7 -5.1,-12.8 -12.8,-12.8h-102.4c-7.7,0 -12.8,5.1 -12.8,12.8v25.6c0,7.7 -5.1,12.8 -12.8,12.8h-89.6z" />
    </group>
</vector>

这里面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));
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容