VectorDrawable是安卓用于svg的描述,优点是体积小,加载快,渲染可能会比Bitmap慢(Bitmap支持缓存和硬件加速)
**
1#添加配置,否则在21版本以下不支持
android {defaultConfig {vectorDrawables.useSupportLibrary = true}}
2#动效使用
anim_color.xml
<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_longAnimTime"android:interpolator="@android:interpolator/linear"android:propertyName="fillColor"android:repeatCount="infinite"android:repeatMode="reverse"android:valueFrom="#7890ff"android:valueTo="#ff8970"android:valueType="intType" />
anim_translate.xml ,移动缩放等动画需要在drawable上添加group标签
<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="@android:integer/config_longAnimTime"android:interpolator="@android:interpolator/linear"android:propertyName="translateX"android:repeatCount="infinite"android:repeatMode="reverse"android:valueFrom="0dp"android:valueTo="5dp"android:valueType="floatType" />
ic_drawable ,注意不能设置tint,否则颜色动效会显示不出
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24.0"android:viewportHeight="24.0"><group android:name="vc"><pathandroid:name="path"android:fillColor="#FF000000"android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" /></group></vector>
vector_anim 把动画和vectorDrawable组合起来
<?xml version="1.0" encoding="utf-8"?><animated-vector xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:drawable="@drawable/ic_drawable"tools:targetApi="lollipop"><targetandroid:animation="@animator/anim_translate"android:name="vc"/><targetandroid:animation="@animator/anim_color"android:name="path"/></animated-vector>
3#Button等控件使用vectorDrawable,不能直接用vector标签,只能用selector间接使用,还需要额外配置
static {AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);}
4#路径clip动画
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24.0"android:viewportHeight="24.0"><pathandroid:name="path"android:strokeAlpha="0.5"android:strokeLineCap="round"android:strokeColor="#789065"android:strokeWidth="1"android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" /></vector>
trimPathStart,表示从开始的位置按百分比开始裁剪,0则全部显示
<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="3000"android:interpolator="@android:interpolator/linear"android:propertyName="trimPathStart"android:repeatCount="infinite"android:repeatMode="reverse"android:valueFrom="0"android:valueTo="1"android:valueType="floatType" />
5#路径转换动画,由一个路径转换到另一个路径(类似的长度和命令数),21以下的版本不支持pathData
<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="3000"android:interpolator="@android:interpolator/linear"android:propertyName="pathData"android:repeatCount="infinite"android:repeatMode="reverse"android:valueFrom="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"android:valueTo="M10,20v-5h3v7h8v-9h1L13,4 2,12h3v8z"android:valueType="pathType" />
<?xml version="1.0" encoding="utf-8"?><animated-vector xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:drawable="@drawable/ic_vector_path"tools:targetApi="lollipop"><targetandroid:animation="@animator/anim_path_change"android:name="path"/></animated-vector>
