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

Android 四大視圖動畫詳解:Alpha、Translate、Rotate 與 Scale

編輯:關於 Android 程式設計

Android 的動畫體系主要分為視圖動畫(View Animation)、屬性動畫(Property Animation)與 Drawable 動畫三大類。本文聚焦最古老也最直覺的一類:視圖動畫。

視圖動畫的核心抽象類是 android.view.animation.Animation,它有五個直接子類:

  • AlphaAnimation:透明度漸變。
  • TranslateAnimation:位移。
  • RotateAnimation:旋轉。
  • ScaleAnimation:伸縮。
  • AnimationSet:把多個動畫組合起來套在同一個 View 上。

動畫既可以用程式碼建立,也可以用 XML 定義。本文的範例都針對一個 TextView 操作,版面大致如下:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="onClick"
        android:text="開始" />

</RelativeLayout>

以下提到的各種座標系,一律以 X 軸正方向水平向右、Y 軸正方向垂直向下為前提。


一、AlphaAnimation:透明度漸變

// 1.0 代表完全不透明,0.0 代表完全透明
float fromAlpha = 1.0f;
float toAlpha = 0.0f;

Animation animation = new AlphaAnimation(fromAlpha, toAlpha);
animation.setDuration(3000);
textView.startAnimation(animation);

建構子只有兩個參數:fromAlpha 是動畫起始的透明度,toAlpha 是結束時的透明度。上例讓 TextView 從完全可見漸變為完全不可見,歷時三秒。

由此可歸納出視圖動畫的通用三步驟:

  1. 建立 Animation 子類的實例。
  2. 用 setDuration() 設定持續時間(毫秒)。
  3. 呼叫 View 的 startAnimation() 啟動。

其他三種動畫流程完全相同,只有建構子參數不同。

改用 XML 定義

視圖動畫的 XML 檔放在 res/anim/ 目錄下,例如 res/anim/alpha.xml:

<!-- res/anim/alpha.xml -->
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" />

載入方式:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
textView.startAnimation(animation);

用 XML 的好處很明顯:要調參數只改資源檔、不動程式碼,可維護性更好。但如果動畫參數必須在執行期才決定,就只能回到程式碼建立。後文每個動畫都附上對應的 XML 寫法。


二、TranslateAnimation:平移動畫

它有兩個常用建構子:

public TranslateAnimation(float fromXDelta, float toXDelta,
                           float fromYDelta, float toYDelta)

public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
                           int fromYType, float fromYValue, int toYType, float toYValue)

基本用法:以像素為單位

int fromXDelta = 0;
int toXDelta = getResources().getDisplayMetrics().widthPixels / 2;
int fromYDelta = 0;
int toYDelta = 0;

Animation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
animation.setDuration(3000);
textView.startAnimation(animation);

進階用法:搭配類型與比例

八參數版本把起始與結束的座標拆成四組(fromX / toX / fromY / toY),每組都由「類型 + 數值」構成。類型有三種:

  • ABSOLUTE:數值代表相對於父容器的絕對像素值。
  • RELATIVE_TO_PARENT:數值是相對於父容器尺寸的百分比。0 代表貼齊父容器左側,0.5 代表父容器水平中線,1 代表與父容器右側重疊。
  • RELATIVE_TO_SELF:數值是相對於自身尺寸的百分比。0 代表原位,0.5 代表向右偏移自身寬度的一半,1 代表剛好偏移自身一整個寬度。
int fromXType = Animation.ABSOLUTE;
float fromXValue = textView.getX();      // 起始 X 與初始位置相同

int toXType = Animation.RELATIVE_TO_PARENT;
float toXValue = 0.5f;                   // 結束 X 落在父容器水平中線

int fromYType = Animation.ABSOLUTE;
float fromYValue = textView.getY();      // 起始 Y 與初始位置相同

int toYType = Animation.RELATIVE_TO_SELF;
float toYValue = 3.0f;                   // 結束 Y 為自身高度的三倍

Animation animation = new TranslateAnimation(
        fromXType, fromXValue, toXType, toXValue,
        fromYType, fromYValue, toYType, toYValue);
animation.setDuration(3000);
textView.startAnimation(animation);

這組參數會讓 TextView 同時向右、向下滑動。

對應的 XML 寫法

XML 中 fromXDelta 等四個屬性的寫尾決定其類型:

  • 無尾碼(例如 android:fromXDelta="0")→ 等同 ABSOLUTE。
  • 以 %p 結尾(例如 android:toXDelta="50%p")→ 等同 RELATIVE_TO_PARENT,p 是 Parent 的縮寫。
  • 以 % 結尾(例如 android:toYDelta="300%")→ 等同 RELATIVE_TO_SELF。

三、RotateAnimation:旋轉動畫

三個常用建構子:

public RotateAnimation(float fromDegrees, float toDegrees)

public RotateAnimation(float fromDegrees, float toDegrees,
                        float pivotX, float pivotY)

public RotateAnimation(float fromDegrees, float toDegrees,
                        int pivotXType, float pivotXValue,
                        int pivotYType, float pivotYValue)

以左上角為旋轉軸

// 順時針旋轉 60 度,預設以 View 左上角為軸
Animation animation = new RotateAnimation(0, 60);
animation.setDuration(3000);
textView.startAnimation(animation);

指定旋轉軸座標

pivotX / pivotY 使用的是 View 自身的局部座標系(原點為 View 左上角):

float pivotX = textView.getWidth() / 2;
float pivotY = textView.getHeight() / 2;

Animation animation = new RotateAnimation(0, 90, pivotX, pivotY);
animation.setDuration(3000);
textView.startAnimation(animation);

若 TextView 為 200 × 100 像素,對應 XML:

<rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromDegrees="0"
    android:toDegrees="90"
    android:pivotX="100"
    android:pivotY="50" />

以類型 + 比例指定旋轉軸

int pivotXType = Animation.RELATIVE_TO_PARENT;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_PARENT;
float pivotYValue = 0.5f;

Animation animation = new RotateAnimation(0, 360,
        pivotXType, pivotXValue, pivotYType, pivotYValue);
animation.setDuration(3000);
textView.startAnimation(animation);

這裡以父容器 RelativeLayout 的中心作為旋轉軸,轉滿一圈。XML 寫法:

<rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%p"
    android:pivotY="50%p" />

四、ScaleAnimation:伸縮動畫

三個常用建構子:

public ScaleAnimation(float fromXScale, float toScaleX,
                       float fromYScale, float toScaleY)

public ScaleAnimation(float fromXScale, float toScaleX,
                       float fromYScale, float toScaleY,
                       float pivotX, float pivotY)

public ScaleAnimation(float fromXScale, float toScaleX,
                       float fromYScale, float toScaleY,
                       int pivotXType, float pivotXValue,
                       int pivotYType, float pivotYValue)

以左上角為縮放中心

float fromXScale = 1.0f;
float toScaleX = 2.0f;     // 水平方向放大一倍
float fromYScale = 1.0f;
float toScaleY = 0.5f;     // 垂直方向縮為一半

Animation animation = new ScaleAnimation(fromXScale, toScaleX, fromYScale, toScaleY);
animation.setDuration(3000);
textView.startAnimation(animation);

以自身中心為縮放中心

float fromXScale = 1.0f;
float toScaleX = 0.5f;
float fromYScale = 1.0f;
float toScaleY = 0.5f;
float pivotX = textView.getWidth() / 2;
float pivotY = textView.getHeight() / 2;

Animation animation = new ScaleAnimation(
        fromXScale, toScaleX, fromYScale, toScaleY, pivotX, pivotY);
animation.setDuration(3000);
textView.startAnimation(animation);

等價的 XML(假設 View 為 200 × 100):

<scale xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromXScale="1"
    android:toXScale="0.5"
    android:fromYScale="1"
    android:toYScale="0.5"
    android:pivotX="100"
    android:pivotY="50" />

也可以用比例寫法,效果完全一樣:

int pivotXType = Animation.RELATIVE_TO_SELF;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_SELF;
float pivotYValue = 0.5f;
<scale xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromXScale="1"
    android:toXScale="0.5"
    android:fromYScale="1"
    android:toYScale="0.5"
    android:pivotX="50%"
    android:pivotY="50%" />

重要觀念釐清

視圖動畫有一個很多人踩過的坑:它只改變「繪製時的視覺呈現」,並不改變 View 真正的位置、大小與點擊區域。一個按鈕位移之後,你仍然要點擊它原本的位置才會觸發事件。

如果需要真正改變屬性、或需要讓點擊區域跟著移動,請改用屬性動畫(ObjectAnimator / ValueAnimator)。此外 AnimationSet 組合多個動畫時,建議加上 setFillAfter(true),否則動畫結束後 View 會瞬間跳回原位。

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