VectorDrawable是安卓用于svg的描述,优点是体积小,加载快,渲染可能会比Bitmap慢(Bitmap支持缓存和硬件加速)
**

1#添加配置,否则在21版本以下不支持

  1. android {
  2. defaultConfig {
  3. vectorDrawables.useSupportLibrary = true
  4. }
  5. }

2#动效使用

anim_color.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="@android:integer/config_longAnimTime"
  4. android:interpolator="@android:interpolator/linear"
  5. android:propertyName="fillColor"
  6. android:repeatCount="infinite"
  7. android:repeatMode="reverse"
  8. android:valueFrom="#7890ff"
  9. android:valueTo="#ff8970"
  10. android:valueType="intType" />

anim_translate.xml ,移动缩放等动画需要在drawable上添加group标签

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="@android:integer/config_longAnimTime"
  4. android:interpolator="@android:interpolator/linear"
  5. android:propertyName="translateX"
  6. android:repeatCount="infinite"
  7. android:repeatMode="reverse"
  8. android:valueFrom="0dp"
  9. android:valueTo="5dp"
  10. android:valueType="floatType" />

ic_drawable ,注意不能设置tint,否则颜色动效会显示不出

  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:width="24dp"
  3. android:height="24dp"
  4. android:viewportWidth="24.0"
  5. android:viewportHeight="24.0">
  6. <group android:name="vc">
  7. <path
  8. android:name="path"
  9. android:fillColor="#FF000000"
  10. android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
  11. </group>
  12. </vector>

vector_anim 把动画和vectorDrawable组合起来

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:drawable="@drawable/ic_drawable"
  5. tools:targetApi="lollipop">
  6. <target
  7. android:animation="@animator/anim_translate"
  8. android:name="vc"/>
  9. <target
  10. android:animation="@animator/anim_color"
  11. android:name="path"/>
  12. </animated-vector>

3#Button等控件使用vectorDrawable,不能直接用vector标签,只能用selector间接使用,还需要额外配置

  1. static {
  2. AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  3. }

4#路径clip动画

  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:width="24dp"
  3. android:height="24dp"
  4. android:viewportWidth="24.0"
  5. android:viewportHeight="24.0">
  6. <path
  7. android:name="path"
  8. android:strokeAlpha="0.5"
  9. android:strokeLineCap="round"
  10. android:strokeColor="#789065"
  11. android:strokeWidth="1"
  12. android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
  13. </vector>

trimPathStart,表示从开始的位置按百分比开始裁剪,0则全部显示

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="3000"
  4. android:interpolator="@android:interpolator/linear"
  5. android:propertyName="trimPathStart"
  6. android:repeatCount="infinite"
  7. android:repeatMode="reverse"
  8. android:valueFrom="0"
  9. android:valueTo="1"
  10. android:valueType="floatType" />

5#路径转换动画,由一个路径转换到另一个路径(类似的长度和命令数),21以下的版本不支持pathData

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="3000"
  4. android:interpolator="@android:interpolator/linear"
  5. android:propertyName="pathData"
  6. android:repeatCount="infinite"
  7. android:repeatMode="reverse"
  8. android:valueFrom="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z"
  9. android:valueTo="M10,20v-5h3v7h8v-9h1L13,4 2,12h3v8z"
  10. android:valueType="pathType" />
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:drawable="@drawable/ic_vector_path"
  5. tools:targetApi="lollipop">
  6. <target
  7. android:animation="@animator/anim_path_change"
  8. android:name="path"/>
  9. </animated-vector>

以上为全部内容!