標籤:table xmlns 起點 直線 啟動 技術 建立 src 切換
在Android 5.X中,Google大量引入線圖動畫。當頁面發生改變時,頁面上的icon不再是生硬的切換,而是通過非常生動的動畫效果,轉換成另一種形態。
要實現的的效果,首先要建立一個靜態svg圖形,即靜態VectorDrawable。
1 <?xml version="1.0" encoding="utf-8"?> 2 <vector xmlns:android="http://schemas.android.com/apk/res/android" 3 android:height="200dp" 4 android:width="200dp" 5 android:viewportWidth="110" 6 android:viewportHeight="110" 7 8 > 9 <group10 android:name="test"11 >12 <path android:strokeColor="@android:color/holo_purple"13 android:strokeWidth="2"14 android:pathData="15 M 20, 8016 L 50, 80 80, 80"17 android:name="path1"18 />19 <path android:strokeColor="@android:color/holo_blue_bright"20 android:strokeWidth="2"21 android:pathData="22 M 20, 2023 L 50, 20 80, 20"24 android:name="path2"25 />26 </group>27 28 </vector>
path1與和path2分別繪製了兩條直線,每條直線有三個點來控制,即起點、中點和終點,形成了初始狀態。
接下來實現path1的變換的objectAnimator動畫。
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android"> 3 <objectAnimator 4 android:duration="1000" 5 android:propertyName="pathData" 6 android:valueFrom=" 7 M 20, 80 8 L 50, 80 80, 80" 9 android:valueTo="M 20, 8010 L 50, 50 80, 20"11 android:valueType="pathType"12 android:interpolator="@android:anim/bounce_interpolator"13 />14 </set>
path2的動畫
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android"> 3 <objectAnimator 4 android:duration="1000" 5 android:propertyName="pathData" 6 android:valueFrom=" 7 M 20, 20 8 L 50, 80 80, 80" 9 android:valueTo="M 20, 2010 L 50, 50 80, 80"11 android:valueType="pathType"12 android:interpolator="@android:anim/bounce_interpolator"13 />14 </set>
這裡需要注意的是:在svg的路徑變換屬性動畫中,變換前後的節點數必須相同,這也是為什麼前面需要使用三個點來繪製一條直線的原因,因為要使用中點來進行動畫變換。
有了VectorDrawable和ObjectAnimator,剩下的只需要使用AnimatedVectorDrawable來將它們粘合在一起。
1 <?xml version="1.0" encoding="utf-8"?> 2 <animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 3 android:drawable="@drawable/demo" 4 > 5 <target 6 android:animation="@animator/anim_path1" 7 android:name="path1"/> 8 <target 9 android:animation="@animator/anim_path2"10 android:name="path2"/>11 </animated-vector>
最後在代碼中啟動動畫即可
1 mImageView = (ImageView) findViewById(R.id.id_imageView);2 ((Animatable)mImageView.getDrawable()).start();
Android-svg動畫樣本之線圖動畫