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

Android Animator 動畫實戰:平移、透明、旋轉與縮放

編輯:關於 Android 程式設計

動畫能讓 UI 更有活力、更具時尚感。Android 中的動畫主要分為兩類:補間動畫(Tween Animation)只需定義開始與結束狀態,中間幀由系統運算補齊;逐幀動畫(Frame Animation)則是把一張張圖片連起來播放。本文聚焦於属性動畫(Animator)體系下的 ObjectAnimator,示範平移、透明、旋轉、縮放四種效果如何用程式碼實現。

動畫能實現的四類效果

  1. 移動(Translation)
  2. 透明度(Alpha)
  3. 旋轉(Rotate)
  4. 縮放(Scale)

一、移動(Translation)

透過 translationX 與 translationY 改變位置,再用 AnimatorSet 串接多段動畫,讓目標「從哪裡出發、最後回到哪裡」:

ObjectAnimator anim  = ObjectAnimator.ofFloat(phone, "translationX", -500f, 0f); // X:從 -500 回到原位
ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "translationX", 0f, -500f); // X:從原位到 -500
ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "translationY", 0f, -500f); // Y:從原位到 -500
ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "translationY", -500f, 0f); // Y:從 -500 回到原位
anim.setDuration(2000);
anim2.setDuration(2000);
anim3.setDuration(2000);
anim4.setDuration(2000);

AnimatorSet set3 = new AnimatorSet();
set3.play(anim4).before(anim3);
AnimatorSet set2 = new AnimatorSet();
set2.play(anim2).before(set3);
AnimatorSet set = new AnimatorSet();
set.play(anim).before(set2);
set.start();

透過 before() 把多段動畫串成一條時間線,就能讓目標沿著 X、Y 軸移動一整圈後回到原點。

二、透明度(Alpha)

ObjectAnimator anim  = ObjectAnimator.ofFloat(phone, "alpha", 1f, 0f); // 由可見到不可見
ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "alpha", 0f, 1f); // 由不可見到可見
anim.setDuration(2000);
anim2.setDuration(2000);

AnimatorSet set = new AnimatorSet();
set.play(anim).before(anim2);
set.start();

三、旋轉(Rotate)

ObjectAnimator anim  = ObjectAnimator.ofFloat(phone, "rotationX", 0f, 180f); // X:0→180
ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "rotationX", 180f, 0f); // X:180→0
ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "rotationY", 0f, 180f); // Y:0→180
ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "rotationY", 180f, 0f); // Y:180→0
anim.setDuration(2000);
anim2.setDuration(2000);
anim3.setDuration(2000);
anim4.setDuration(2000);

AnimatorSet set = new AnimatorSet();
set.play(anim).before(anim2);
set.play(anim3).before(anim4); // 這樣寫可讓 X、Y 同時旋轉
set.start();

四、縮放(Scale)

ObjectAnimator anim  = ObjectAnimator.ofFloat(phone, "scaleX", 1f);   // 縮小 X
ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "scaleX", 0.5f);
ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "scaleY", 1f);   // 縮小 Y
ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "scaleY", 0.5f);
ObjectAnimator anim5 = ObjectAnimator.ofFloat(phone, "scaleX", 0.5f); // 放大 X
ObjectAnimator anim6 = ObjectAnimator.ofFloat(phone, "scaleX", 1f);
ObjectAnimator anim7 = ObjectAnimator.ofFloat(phone, "scaleY", 0.5f); // 放大 Y
ObjectAnimator anim8 = ObjectAnimator.ofFloat(phone, "scaleY", 1f);
// 依序設定 duration 後,用 AnimatorSet 串接:先縮小、再放大

小結

這四種效果的共同套路是:用 ObjectAnimator.ofFloat() 指定要動的屬性(translationX、alpha、rotationX、scaleX…)與起訖值,設定 duration,再用 AnimatorSet 的 play().before() 或 with() 編排先後與並行關係,最後 start()。熟悉這套組合,就能變化出豐富流暢的動態效果。

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