trimPathEnd/trimPathStart 軌跡動畫
VectorDrawable 加上trimPathStart ,可以做到向下圖這樣,線由起點(0%)縮短至終點(100%)
步驟1:新增藍色的線
drawable/line.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
android:width="24dp">
<path
android:name="line"
android:strokeColor="#0c6bf9"
android:strokeWidth="1.5"
android:pathData="M0,20 L24,20"/>
</vector>
步驟2:加上動畫 trimPathStart
線由起點(0%)縮短至終點(100%)
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="trimPathStart"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType" />
animated-vector 為drawable/line指定動畫animator/anim_trim_path_end_0_to_1
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/line">
<target
android:name="line"
android:animation="@animator/anim_trim_path_end_0_to_1" />
</animated-vector>
trimPathStart、trimPathEnd的組合如下:
trimPathStart。valueFrom:0, valueTo:1
線由起點(0%)縮短至終點(100%)
trimPathStart。valueFrom:1, valueTo:0
線由終點增長至起點
trimPathEnd。valueFrom:0,valueTo:1
線由起點增長至終點
trimPathEnd。valueFrom:1,valueTo:0
線由終點縮短至起點
完整程式:https://github.com/evanchen76/VectorAnimationTrimPath