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

Android 動畫完全指南:Tween 補間動畫與 Frame 影格動畫

編輯:關於 Android 程式設計

Android 提供了兩套動畫體系:Tween 動畫(補間動畫)與 Frame 動畫(影格動畫)。前者擅長讓同一個 View 產生平移、縮放、旋轉、淡入淡出等連續變化;後者則像放電影膠卷一樣,依序播放預先準備好的圖片。兩者都可以用 XML 宣告,也可以用程式碼動態建立。

一、Tween 補間動畫

Tween 動畫的本質是對 View 內容做一系列矩陣變換,四種基本型別分別對應四個 Java 類別:AlphaAnimation(透明度)、ScaleAnimation(縮放)、TranslateAnimation(平移)、RotateAnimation(旋轉)。若要組合多種效果,則用 AnimationSet。

步驟一:在 res/anim 下定義動畫

先在專案的 res 目錄建立 anim 資料夾,再新增 XML 檔(例如 scale.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:toXScale="5"
        android:toYScale="5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="5000" />
</set>

屬性說明

  • fromXScale / fromYScale:動畫起始時的縮放比例。0.0 表示縮到消失,1.0 表示原始大小,小於 1 為縮小,大於 1 為放大。
  • toXScale / toYScale:動畫結束時的縮放比例。
  • pivotX / pivotY:變化的基準點,以百分比表示。50% 即為 View 的中心。
  • duration:持續時間,單位為毫秒。
  • fillAfter:設為 true 時,動畫結束後仍套用最後一幀的轉換結果。
  • repeatCount / repeatMode:重複次數與重複模式(reverse 可做出來回擺盪的效果)。

速度曲線由 interpolator 決定。系統內建了 LinearInterpolator(等速)、AccelerateInterpolator(加速)、DecelerateInterpolator(減速)、AccelerateDecelerateInterpolator(先加速後減速)等;也可以自行繼承實作拋物線、自由落體等物理曲線。

步驟二:在版面中放一個 View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon" />
</LinearLayout>

不限於 ImageView,任何 View(包含 TextView)都能套用補間動畫。

步驟三:在 Activity 中啟動

public class AnimationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
        imageView.startAnimation(animation);
    }
}

等價的純程式碼寫法:

ScaleAnimation animation = new ScaleAnimation(
    0.0f, 5f, 0.0f, 5f,
    Animation.RELATIVE_TO_SELF, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(5000);
imageView.startAnimation(animation);

其他常見效果

淡入(透明度變化)

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="3000" />
</set>
AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
animation.setDuration(5000);
imageView.startAnimation(animation);

位移

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:repeatCount="2">
    <translate
        android:fromXDelta="0" android:fromYDelta="0"
        android:toXDelta="120" android:toYDelta="120"
        android:duration="3000" />
</set>
TranslateAnimation animation = new TranslateAnimation(0, 120, 0, 120);
animation.setDuration(5000);
imageView.startAnimation(animation);

旋轉

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate
        android:interpolator="@android:anim/accelerate_interpolator"
        android:repeatCount="2"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000" />
</set>

角度的正負決定方向:由負轉為正值順時針,由正轉為負值逆時針;toDegrees 可以大於 360 以做出多圈旋轉。

RotateAnimation animation = new RotateAnimation(
    0, -90,
    Animation.RELATIVE_TO_SELF, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(500);
imageView.startAnimation(animation);

動畫集合

<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="90" />
    <scale android:duration="2000"
        android:fromXScale="0.0" android:fromYScale="0.0"
        android:repeatCount="4" android:repeatMode="reverse"
        android:toXScale="2.0" android:toYScale="2.0" />
</set>

完整示範的 Activity:

package cn.itcast.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class DemoActivity extends Activity {

    private ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        iv = (ImageView) this.findViewById(R.id.iv);
    }

    public void alpha(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
        iv.startAnimation(aa);
    }

    public void rotate(View view) {
        Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);
        iv.startAnimation(ra);
    }

    public void scale(View view) {
        Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);
        iv.startAnimation(sa);
    }

    public void translate(View view) {
        Animation ta = AnimationUtils.loadAnimation(this, R.anim.translate);
        iv.startAnimation(ta);
    }

    /** 動畫組合 */
    public void set(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.set);
        iv.startAnimation(aa);
    }
}

對應的版面檔用五顆按鈕觸發不同效果:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="alpha" android:text="透明度" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="rotate" android:text="旋轉" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="scale" android:text="縮放" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="translate" android:text="平移" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:onClick="set" android:text="組合動畫" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

二、Frame 影格動畫

Frame 動畫就是把多張圖片排成一列,依設定的時間間隔輪流顯示,效果等同逐格播放的動畫。開發步驟:

  1. 把準備好的圖片放進 res/drawable。
  2. 在 res/anim(或 drawable)下建立動畫 XML,也可以直接用 AnimationDrawable 以程式碼建立。
  3. 為 View 綁定動畫,呼叫 AnimationDrawable.start() 開始播放。

定義影格動畫

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/girl_1" android:duration="200" />
    <item android:drawable="@drawable/girl_2" android:duration="200" />
    <item android:drawable="@drawable/girl_3" android:duration="200" />
</animation-list>

android:oneshot 為 true 時動畫只播一次並停在最後一幀;設為 false 則循環播放。每個 <item> 的 duration 就是該幀的停留毫秒數。

綁定與播放

public class FrameActivity extends Activity {

    private AnimationDrawable animationDrawable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.setBackgroundResource(R.anim.frame);
        animationDrawable = (AnimationDrawable) imageView.getBackground();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            animationDrawable.start();
            return true;
        }
        return super.onTouchEvent(event);
    }
}

這裡有個關鍵細節:animationDrawable.start() 不能寫在 onCreate() 裡。因為在 onCreate 階段,AnimationDrawable 尚未完成與 ImageView 的綁定,此時啟動動畫只會看到第一張圖片靜止不動。必須等到 View 完成配置之後(例如使用觸摸事件、或 View.post() 延後執行)才能正常播放。

多幀完整範例

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/girl_1"  android:duration="100" />
    <item android:drawable="@drawable/girl_2"  android:duration="100" />
    <item android:drawable="@drawable/girl_3"  android:duration="100" />
    <item android:drawable="@drawable/girl_4"  android:duration="100" />
    <item android:drawable="@drawable/girl_5"  android:duration="100" />
    <item android:drawable="@drawable/girl_6"  android:duration="100" />
    <item android:drawable="@drawable/girl_7"  android:duration="150" />
    <item android:drawable="@drawable/girl_8"  android:duration="100" />
    <item android:drawable="@drawable/girl_9"  android:duration="200" />
    <item android:drawable="@drawable/girl_10" android:duration="200" />
    <item android:drawable="@drawable/girl_11" android:duration="200" />
</animation-list>

注意根標籤是 <animation-list>,而不是 <set>——後者是補間動畫用的。

兩者該怎麼選

如果目標物件是「一個 View 的屬性變化」,用 Tween 就夠了,成本低、寫法直觀。如果需要「逐格播放的畫面」,例如 Loading 動圖或角色行走,就該用 Frame。要留意 Frame 動畫會把所有幀的 Bitmap 都載入記憶體,幀數多、解析度大時容易 OOM,必要時應改用自訂 SurfaceView 或第三方動畫庫串流播放。

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