Android Animation學習(六) View Animation介紹
View Animation
View animation系統可以用來執行View上的Tween animation和Frame animation。
Tween animation可以在View對象上執行一系列的簡單變換,比如位置、尺寸、旋轉、透明度等。
animation package
包中包含了tween animation所有的類。
一系列的動畫命令定義了一個完整的tween animation,可以用代碼定義也可以用XML資源文件定義。
XML資源文件
XML資源文件的使用可以見:Animation Resources。
XML文件放在項目的res/anim/目錄下。文件必須有一個唯一的根節點。
這個根節點可以是:<alpha>, <scale>, <translate>, <rotate>, interpolator element, 或者是<set>。
默認情況下,所有的動畫都是並行進行的,要想使得它們順尋發生,你必須指定startOffset屬性。
有一些值,可以指定是相對於View本身還是相對於父類容器的。
比如pivotX,要表示相對於自身的50%,要用50%;要表示相對於父類容器的50%,則直接寫50。
使用例子
XML文件存儲為:res/anim/hyperspace_jump.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="700" /> <set android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="700"> <scale android:fromXScale="1.4" android:toXScale="0.0" android:fromYScale="0.6" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="400" /> <rotate android:fromDegrees="0" android:toDegrees="-45" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="400" /> </set> </set>
在代碼中把這個動畫應用於一個ImageView:
ImageView image = (ImageView) findViewById(R.id.image); Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); image.startAnimation(hyperspaceJump);
除了調用 startAnimation()
,另一種處理方式是通過Animation.setStartTime()方法定義一個開始時間,然后通過View.setAnimation()
方法把這個動畫賦給控件即可。
View Animation和Property Animation
View Animation是API Level 1就引入的。
View Animation在包android.view.animation
中。
動畫類叫Animation。
Property Animation是API Level 11引入的,即Android 3.0才開始有Property Animation相關的API。
Property Animation API在包 android.animation
中。
動畫相關類叫Animator。
參考資料
API Guides:View Animation
http://developer.android.com/guide/topics/graphics/view-animation.html
Tween animation的包:
http://developer.android.com/reference/android/view/animation/package-summary.html
Animation類:
http://developer.android.com/reference/android/view/animation/Animation.html
Animation Resources
http://developer.android.com/guide/topics/resources/animation-resource.html