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

Android Animation 動畫 Demo:Tween 動畫四兄弟與組合動畫

編輯:關於 Android 程式設計

Android 內建的動畫體系主要有兩套:Tween 動畫(補間動畫)與 Frame 動畫(影格動畫)。本文聚焦在 Tween,Frame 留待下一篇。

Tween 動畫是什麼

它做法是對 View 的內容施加一連串的圖形變換——平移、縮放、旋轉、改變透明度——从而产生动感。定義方式有兩種:寫在 XML 裡,或直接用程式碼建立對應的 Animation 物件。兩種做法文末都有範例。

四種基本動畫

效果XML 標籤Java 類別
漸變透明度(透明動畫)<alpha>AlphaAnimation
漸變尺寸(縮放動畫)<scale>ScaleAnimation
位置移動(移位動畫)<translate>TranslateAnimation
畫面旋轉(旋轉動畫)<rotate>RotateAnimation

一、版面檔

用六個按鈕分別觸發單一動畫與組合動畫:

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:text="使用 XML 檔案來實現動畫"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="20dp" />

    <Button android:id="@+id/button_alphaAnim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="透明動畫 AlphaAnimation" />
    <Button android:id="@+id/button_rotateAnim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="旋轉動畫 RotateAnimation" />
    <Button android:id="@+id/button_translateAnim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="移位動畫 TranslateAnimation" />
    <Button android:id="@+id/button_scaleAnim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="縮放動畫 ScaleAnimation" />
    <Button android:id="@+id/button_setAnim1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="透明動畫 + 移位動畫" />
    <Button android:id="@+id/button_setAnim2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="旋轉動畫 + 縮放動畫" />
</LinearLayout>

二、res/anim 下的動畫定義

旋轉動畫 ra.xml

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

pivotX/Y 是旋轉中心,用百分比表示相對於自身尺寸的相對位置;50% 就是繞著正中央轉。

縮放動畫 sa.xml

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

從 0 倍放大到 1 倍(原始大小),效果就是「從中心點彈出來」。

透明動畫 aa.xml

<alpha xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />

移位動畫 ta.xml

<translate xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="100%p"
    android:toYDelta="0" />

這裡有個容易搞錯的地方:%p 是相對於父容器,% 則是相對於自身。寫錯單位,位移距離會差好幾倍。

組合動畫 set1.xml(透明 + 移位)

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:shareInterpolator="true">
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" />
    <translate android:fromXDelta="0" android:toXDelta="100%p" />
</set>

組合動畫 set2.xml(旋轉 + 縮放)

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:shareInterpolator="true">
    <rotate android:fromDegrees="0"
            android:pivotX="50%" android:pivotY="50%"
            android:toDegrees="360" />
    <scale android:fromXScale="0" android:fromYScale="0"
           android:pivotX="50%" android:pivotY="50%"
           android:toXScale="1" android:toYScale="1" />
</set>

shareInterpolator="true" 表示子動畫共用同一個插值器;設為 false 時各自獨立,時序控制會更靈活,也能搭配 android:startOffset 做出接力效果。

三、Activity 程式碼

package com.myanimationdemo2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    private Button button_alphaAnim, button_rotateAnim, button_translateAnim,
                   button_scaleAnim, button_setAnim1, button_setAnim2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button_alphaAnim     = (Button) findViewById(R.id.button_alphaAnim);
        button_rotateAnim    = (Button) findViewById(R.id.button_rotateAnim);
        button_translateAnim = (Button) findViewById(R.id.button_translateAnim);
        button_scaleAnim     = (Button) findViewById(R.id.button_scaleAnim);
        button_setAnim1      = (Button) findViewById(R.id.button_setAnim1);
        button_setAnim2      = (Button) findViewById(R.id.button_setAnim2);

        button_alphaAnim.setOnClickListener(this);
        button_rotateAnim.setOnClickListener(this);
        button_translateAnim.setOnClickListener(this);
        button_scaleAnim.setOnClickListener(this);
        button_setAnim1.setOnClickListener(this);
        button_setAnim2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_alphaAnim:
                v.startAnimation(AnimationUtils.loadAnimation(
                        getApplicationContext(), anim.aa));
                break;
            case R.id.button_rotateAnim:
                v.startAnimation(AnimationUtils.loadAnimation(
                        getApplicationContext(), anim.ra));
                break;
            case R.id.button_translateAnim:
                v.startAnimation(AnimationUtils.loadAnimation(
                        getApplicationContext(), anim.ta));
                break;
            case R.id.button_scaleAnim:
                v.startAnimation(AnimationUtils.loadAnimation(
                        getApplicationContext(), anim.sa));
                break;
            case R.id.button_setAnim1:
                v.startAnimation(AnimationUtils.loadAnimation(
                        getApplicationContext(), anim.set1));
                break;
            case R.id.button_setAnim2:
                v.startAnimation(AnimationUtils.loadAnimation(
                        getApplicationContext(), anim.set2));
                break;
            default:
                break;
        }
    }
}

整個流程只有兩個關鍵 API:AnimationUtils.loadAnimation() 把 XML 讀成 Animation 物件,View.startAnimation() 把它套上去。就這麼簡單。

四、用程式碼寫等效的動畫

若不寫 XML,直接建立物件即可,以旋轉為例:

RotateAnimation rotate = new RotateAnimation(
        0f, 360f,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(2000);
button_rotateAnim.startAnimation(rotate);

程式碼方式的好處是參數可以在執行期動態計算,適合資料驅動的動畫;XML 方式則更適合固定、可預覽的動效,也方便交給設計師調整。

五、幾個實務提醒

  • Tween 只改「看起來的位置」,不改真正的位置。動畫結束後,View 的實際邊界並沒有變,所以點擊事件仍落在原本的位置。這是新手最常踩的坑。若要真正改變位置,得用 ObjectAnimator(屬性動畫)或事後更新 LayoutParams。
  • 記得加插值器。預設是 AccelerateDecelerateInterpolator,但依情境換成 OvershootInterpolator(回彈)或 AnticipateOvershootInterpolator 往往更有活力。
  • 動畫不影響版面配置。因此不要用 Tween 動畫來做捲動或進度推進這類需要真實位置變化的效果。

以上為 Tween 動畫以 XML 形式實作的完整範例。XML 版與純程式碼版的 Demo 源碼都放在文末供下載。

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