在Android開發中,可以使用VectorDrawable和AnimatedVectorDrawable類來實現矢量圖形和矢量動畫。
<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:fillColor="#FF0000"
android:pathData="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z"/>
</vector>
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="path"
android:fillColor="#FF0000"
android:pathData="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z"/>
</vector>
animated_vector_drawable.xml:
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_drawable">
<target
android:name="path"
android:animation="@anim/path_animation"/>
</animated-vector>
path_animation.xml:
<objectAnimator
android:duration="1000"
android:propertyName="pathData"
android:valueFrom="M12,2C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,2z"
android:valueTo="M12,10C6.48,2,2,6.48,2,12s4.48,10,10,10s10-4.48,10-10S17.52,2,12,10z"/>
以上示例中,animated_vector_drawable.xml中定義了一個AnimatedVectorDrawable,并指定了動畫過渡的目標路徑和對應的屬性動畫。通過在代碼中加載這個AnimatedVectorDrawable并播放動畫,就可以實現矢量圖形的動態變化效果。