單一動畫:
- 創建不同的animation對象
- 確定動畫時間
- view開始動畫
複雜動畫:
1、動畫集
- 創建animation set對象
- set對象加入不同的animation對象
- set對象設定時間或者不同的animation對象分別設定時間
- 開始動畫
2、逐幀動畫
- 創建AnimationDrawable對象
- 向對象添加資源
- 向view對象增加AnimationDrawable顯示
- 開始動畫
示例代碼:
動畫集:
<span style="font-size:14px;"> AnimationSet animation = new AnimationSet(Activity07.this,
null);
Animation a1 = new RotateAnimation(0, 360 * 3,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Animation a2 = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Animation a3 = new RotateAnimation(0, -360 * 2,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
a1.setDuration(3000);
a2.setDuration(1000);
a3.setDuration(1000);
a3.setStartOffset(3000);
animation.addAnimation(a1);
animation.addAnimation(a2);
animation.addAnimation(a3);
imageView.startAnimation(animation);</span>
逐幀動畫:
<span style="font-family:Microsoft YaHei;font-size:14px;"> AnimationDrawable animDrawable = new AnimationDrawable();
for (int i = 0; i < 9; i++) {
int resId = getResources().getIdentifier("img_item" + i,
"drawable", "com.tarena.tts");
animDrawable.addFrame(getResources().getDrawable(resId),
125);
}
animDrawable.setOneShot(false);
imageView.setImageDrawable(animDrawable);
animDrawable.start();</span>