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

Android 動畫詳解之 View 動畫(Tween Animation)

編輯:關於 Android 程式設計

為了讓互動更順暢,動畫在某些場景下是必要的。Android 3.0 之前支援兩種動畫:Tween 動畫與 Frame 動畫。前者負責平移、縮放、旋轉、透明度漸變這類變換,後者則像 GIF 一樣靠一連串圖片模擬動畫。3.0 之後又引入了屬性動畫(Property Animation),能力更強、控制更細。

這篇聚焦在 Tween 動畫,也就是俗稱的 View 動畫。

一、先認識 View 動畫的根本限制

View 動畫只能作用在 View 物件上,且只支援部分屬性。更關鍵的一點是:它只改變了 View 被繪製的位置,並未改變 View 本身。

舉例來說,一個按鈕原本座標是 (200, 200),你用平移動畫把它移到 (200, 500)。視覺上按鈕在新位置,但實際的點擊事件區域仍留在 (200, 200)——點擊移動後的按鈕不會有任何反應。

理解這個前提,才能判斷該用 View 動畫還是屬性動畫。

二、四種基本動畫

寫法有 XML 與 Java 兩種,兩者可以互相轉換。

1. 平移動畫 Translate

XML 的常用屬性:duration(動畫時間)、fillAfter(是否保持結束狀態)、fromXDelta / fromYDelta(起始位置)、toXDelta / toYDelta(結束位置)、repeatCount(重複次數)、repeatMode(重複模式,restart 正序、reverse 倒序)。

在程式碼中載入並啟動:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.tran_btn);
view.startAnimation(animation);

或者直接建立物件:

Animation animation2 = new TranslateAnimation(0, 20, 0, 0);
animation2.setDuration(2000);
animation2.setRepeatCount(10);
animation2.setRepeatMode(Animation.RESTART);
button.startAnimation(animation2);

建構子參數依序為 TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)。

2. 旋轉動畫 Rotate

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%" />

pivotX / pivotY 是旋轉中心點,設為 50% 表示繞自身中心旋轉。

Animation animation2 = new RotateAnimation(0, 360, 0, 0);
animation2.setDuration(2000);
button.startAnimation(animation2);

3. 漸變動畫 Alpha

Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(3000);
button.startAnimation(animation);

參數為起始與結束的透明度,1.0 為完全不透明,0.0 為全透明。

4. 縮放動畫 Scale

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

把四種看通,其餘的變化都能觸類旁通。

三、組合動畫 set

要同時播放多個動畫,用 <set> 包起來:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android" >
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="2000" />
</set>

不加 startOffset 時,子動畫同時開始。若要依序播放,為後面的動畫加上 android:startOffset 指定延遲時間:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android" >
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
    <alpha
        android:startOffset="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="2000" />
</set>

四、插值器 Interpolator:控制速度曲線

預設的運動是線性的,看起來會很生硬。透過插值器可以改變速度變化:

animation.setInterpolator(new AccelerateInterpolator());

系統內建的九種插值器:

  • AccelerateDecelerateInterpolator:頭尾慢、中段快,最自然的節奏,也是預設值。
  • AccelerateInterpolator:起步慢,之後逐漸加速。
  • DecelerateInterpolator:起步快,之後逐漸減速。
  • AnticipateInterpolator:先往反方向退一小段,再向前衝。
  • AnticipateOvershootInterpolator:先向後退,再衝過目標值,最後回到終點。
  • OvershootInterpolator:衝過目標值後再回到原點。
  • BounceInterpolator:結束時像球落地般彈跳一下。
  • CycleInterpolator:以正弦曲線重複播放指定次數。
  • LinearInterpolator:等速變化。

五、View 動畫何時不夠用

回到開頭提到的限制。以下情況應該改用屬性動畫(ObjectAnimator / ValueAnimator):

  • 動畫結束後,View 需要能在新位置接收點擊事件。
  • 需要精確控制動畫的播放、暫停、復播與串接。
  • 動畫對象不是 View,而是任意物件的屬性。
  • 需要在動畫過程中監聽每一幀的插值數值。

屬性動畫真正的改動了物件的屬性值,因此上述問題都不存在。Android 3.0 之後,新專案幾乎都應該優先考慮屬性動畫;若需相容舊版本,則可引入 androidx 的相容庫。

謝謝耐心看完。不積跬步無以至千里,有疑問歡迎留言。

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