Android的動畫實現是在Animation裡面實現的,在Android裡面,有兩種Animation模式:
其中Tween Animation是通過對場景裡的對象不斷做圖像變換(平移、縮放、旋轉)產生動畫效果,即是一種漸變動畫;而Frame Animation:順序播放事先做好的圖像,是一種畫面轉換動畫。
下面是一個Android Animation的配置文件例子。相對比較簡單。但是基本方法都有用到:
[html]
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
>
<!– 定義縮放變換 –>
<scale
android:fromXScale="1.0"
android:toXScale="1.4"
android:fromYScale="1.0"
android:toYScale="0.6"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:duration="2000"
/>
<!– 定義位移變化 –>
<translate
android:fromXDelta="10"
android:toXDelta="130"
android:fromYDelta="30"
android:toYDelta="-80"
android:duration="2000"
/>
</set>
在Set裡面可以定義四種類型
alpha 漸變透明度動畫效果
scale 漸變尺寸伸縮動畫效果
translate 畫面轉換位置移動動畫效果
rotate 畫面轉移旋轉動畫效果
當然,Android隻要支持XML的肯定支持Java代碼裡面寫,這四個類肯定是上面的類型加Animation(AlphaAnimation等),效果一樣。個人偏好XML配置。直觀。
下面是兩種模式Animation的實現:
1.Tween Animation
一個tween動畫就是變化一系列的動作的組合。如果你有一個EditText對象,你可以移動它,旋轉它,讓它變大或讓它變小,如果文字下面還有背景圖像,背景圖像也會隨著文件進行轉換。
使用XML來定義Tween Animation
動畫的XML文件在工程中res/anim目錄,這個文件必須包含一個根元素,可以使上面的四個屬性值進行描述。或者是把上面的元素都放入<set>元素組中,默認情況下,所以的動畫指令都是同時發生的,但是,如果要讓他們按照順序發生,則必須設置startOffset。動畫的指令定義瞭你想要發生什麼樣的轉換,當他們發生瞭,應該執行多長時間,轉換可以是連續的也可以使同時的。例如,你讓文本內容從左邊移動到右邊,然後旋轉180度,或者在移動的過程中同時旋轉,沒個轉換需要設置一些特殊的參數(開始和結束的大小尺寸的大小變化,開始和結束的旋轉角度等等,也可以設置些基本的參數(例如,開始時間與周期),如果讓幾個轉換同時發生,可以給它們設置相同的開始時間,如果按序列的話,計算開始時間加上其周期。
<set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
<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/decelerate_interpolator">
<scale
android:fromXScale="1.4"
android:toXScale="0.0"
android:fromYScale="0.6"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400"
android:fillBefore="false" />
<rotate
android:fromDegrees="0"
android:toDegrees="-45"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:duration="400" />
</set>
</set>
Tween Animation如何使用
使用AnimationUtils類的靜態方法loadAnimation()來加載XML中的動畫XML文件
//main.xml中的ImageView
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
//加載動畫
Animation hyperspaceJumpAnimation =AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
//使用ImageView顯示動畫
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
如何在Java代碼中定義動畫
//在代碼中定義 動畫實例對象
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;
//根據各自的構造方法來初始化一個實例對象
myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
對於interpolator的解釋
interpolator定義一個動畫的變化率(the rate of change)。這使得基本的動畫效果(alpha, scale, translate, rotate)得以加速,減速,重復等。
Interpolator 定義瞭動畫的變化速度,可以實現勻速、正加速、負加速、無規則變加速等。Interpolator 是基類,封裝瞭所有 Interpolator 的共同方法,它隻有一個方法,即 getInterpolation (float input),該方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供瞭幾個 Interpolator 子類,實現瞭不同的速度曲線,如下:
AccelerateDecelerateInterpolator | 在動畫開始與介紹的地方速率改變比較慢,在中間的時候加速 |
AccelerateInterpolator | 在動畫開始的地方速率改變比較慢,然後開始加速 |
CycleInterpolator | 動畫循環播放特定的次數,速率改變沿著正弦曲線 |
DecelerateInterpolator | 在動畫開始的地方速率改變比較慢,然後開始減速 |
LinearInterpolator | 在動畫的以均勻的速率改變 |
Frame Animation
Frame Animation是順序播放事先做好的圖像,跟電影類似。不同於animation package, Android SDK提供瞭另外一個類AnimationDrawable來定義、使用Frame Animation。
Frame Animation可以在XML Resource定義(還是存放到res\anim文件夾下),也可以使用AnimationDrawable中的API定義。由於Tween Animation與Frame Animation有著很大的不同,因此XML定義的格式也完全不一樣,其格式是:首先是animation-list根節點,animation-list根節點中包含多個item子節點,每個item節點定義一幀動畫,當前幀的drawable資源和當前幀持續的時間。下面對節點的元素加以說明:
下面就給個具體的XML例子,來定義一幀一幀的動畫:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
然後我們將以上XML保存在res/anim/文件夾下,命名為rocket_thrust.xml,顯示動畫的代碼:
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.anim.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
摘自 Jason的Java專欄