Android 基本AnimatedVectorDrawable

示例

一个AnimatedVectorDrawable需要至少3个部分组成:

  • 一个VectorDrawable将被操纵的

  • 一个objectAnimator定义要更改的属性以及如何更改

  • 将AnimatedVectorDrawable其连接本身objectAnimator的VectorDrawable创建动画

以下内容创建了一个三角形,将其颜色从黑色转换为红色。

的VectorDrawable,文件名:triangle_vector_drawable.xml

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

    <path
        android:name="triangle"
        android:fillColor="@android:color/black"
        android:pathData="M0,24 l12,-24 l12,24 z"/>

</vector>

的objectAnimator,文件名:color_change_animator.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="fillColor"
    android:duration="2000"
    android:repeatCount="infinite"
    android:valueFrom="@android:color/black"
    android:valueTo="@android:color/holo_red_light"/>

的AnimatedVectorDrawable,文件名:triangle_animated_vector.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/triangle_vector_drawable">

    <target
        android:animation="@animator/color_change_animator"
        android:name="triangle"/>

</animated-vector>

请注意,<target>指定android:name="triangle"与<path>中的匹配VectorDrawable。AVectorDrawable可以包含多个元素,并且该android:name属性用于定义要定位的元素。

结果: