本文參考:
http://developer.android.com/guide/topics/resources/animation-resource.html
http://developer.android.com/guide/topics/graphics/animation.html
在Android中,Animation分為Tween Animation和Frame Animation兩類。
一、Tween Animation
對一張圖片進行一系列的變換(包括縮放、透明度、移動、旋轉)。對應的類是Animation;
資源訪問方式:
In Java: R.anim.filename
In XML: @[package:]anim/filename
文件位置:
res/anim/filename.xml
語法:
<?xml version="1.0" encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >
<alpha
android:fromAlpha="float"
android:toAlpha="float"/>
<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float"/>
<translate
android:fromXDelta="float"
android:toXDelta="float"
android:fromYDelta="float"
android:toYDelta="float"/>
<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float"/>
<set>
...
</set>
</set>
set節點:對應AnimationSet類
android:interpolator:是否為這個Animation定義一個Interpolator(負責定義Animation的變化速率)。
android:shareInterpolator:是否讓子節點這個Interpolator。
alpha節點:對應AlphaAnimation類
用於設置Animation的透明度。
android:fromAlpha:起始時的透明度。0.0:透明;1.0:不透明。
android:toAlpha:結束時的透明度。0.0:透明;1.0:不透明。
scale節點:對應ScaleAnimation類
用於設置Animation縮放。
android:fromXScale:起始時X方向的縮放程度,使用百分比。
android:toXScale:結束時X方向的縮放程度,使用百分比。
android:fromYScale:起始時Y方向的縮放程度,使用百分比。
android:toYScale:結束時Y方向的縮放程度,使用百分比。
android:pivotX:縮放時的基准點在X方向的位置,可以使用浮點數或百分比
android:pivotY:縮放時的基准點在Y方向的位置,可以使用浮點數或百分比
translate節點:對應TranslateAnimation類
用於設置Animation的移動。
android:fromXDelta:起始位置的X方向的坐標,可以使用浮點數或百分比
android:toXDelta:結束位置的X方向的坐標,可以使用浮點數或百分比
android:fromYDelta:起始位置的Y方向的坐標,可以使用浮點數或百分比
android:toYDelta:結束位置的Y方向的坐標,可以使用浮點數或百分比
rotate節點:對應RotateAnimation類
用於設置Animation的旋轉。
android:fromDegrees:開始旋轉時的角度,使用浮點數
android:toDegrees:結束旋轉的角度,使用浮點數
android:pivotX:旋轉基准點的X方向的坐標,使用浮點數或百分比
android:pivotY:旋轉基准點的Y方向的坐標,使用浮點數或百分比
如何確定旋轉的方向:
我們假設fromDegrees=A;toDegrees=B:
如果A>B,則逆時針旋轉;
如果A<B,則順時針旋轉。
注:在translate和rotate中使用百分比時能針對自身和父控件來確定位置:①距離自己左、上邊界(右上角為基准點)的百分比,如:"5%";②
距離父控件的左、上邊界的百分比,如:"5%p"
.
interpolators:
請參考:http://developer.android.com/guide/topics/resources/animation-resource.html#Interpolators
在定義Tween Animation時還有兩個比較重要的屬性是:android:duration(動作的持續時間)和android:startOffset(動作的開始時間).
二、Frame Animation
將幾張圖片按順序進行顯示。對應的類是AnimationDrawable;
資源訪問方式:
In Java: R.drawable.filename
In XML: @[package:]drawable.filename
文件位置:
res/drawable/filename.xml
語法:
<?xml version="1.0" encoding="utf-8"?>
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer"/>
</animation-list>
animation-list節點:
必需的,且必須是根節點,包含一個或多個item節點.
android:oneshot:是否要循環顯示該動畫.
item節點:
Frame Animation的一個frame.必須包含在animation-list節點.
android:drawable:要顯示的圖片
android:duration:顯示時間,計算單位是毫秒.
Demo(基於Android2.2;UTF-8編碼):
http://dl.dbank.com/c0r6pvdtfr