Android-svg動畫樣本之線圖動畫

來源:互聯網
上載者:User

標籤: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動畫樣本之線圖動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.