Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android AnimationSet 小結:組合多個動畫

編輯:關於 Android 程式設計

在 Android 中開發複合動畫時,需要使用 AnimationSet 類別。透過它的 add 方法即可加入多個動畫,也可以在佈局檔中用 <set> 標籤一次加入多個動畫——不過這種方式下的多個動畫會在同一時間一起開始。

XML 方式

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
</set>

程式碼方式

AnimationSet 提供了一個把多個動畫組合成一個組合的機制,並可設定組中動畫的時序關係,例如同時播放、順序播放等。以下示範同時應用 5 個動畫:

// 播放 anim1;
// 同時播放 anim2、anim3、anim4;
// 播放 anim5。
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4);
bouncer.play(anim5).after(anim2);
bouncer.start();

其中 before、with、after 分別用來描述動畫之間的「先後」與「同時」關係,靈活搭配即可編排出豐富的複合動畫效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved