基本的 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",它與 VectorDrawable 中的 <path> 相匹配。VectorDrawable 可能包含多個元素,android:name 屬性用於定義要定位的元素。
結果: