Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android 遊戲開發教學(十八):AnimationDrawable 逐格動畫實作

編輯:Android 遊戲開發

Android 在製作 2D 逐格動畫時,提供了以 XML 描述動畫檔的方式。也就是說,底層已經把播放介面備好了,我們要做的只是照規則把影格排好、再呼叫它。本文就來拆解整個流程。

一、用 XML 排出一段逐格動畫

首先在專案的 res 資源目錄下建立 anim 資料夾,並在其中新增 animation.xml,路徑即為 res/anim/animation.xml。

<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/a" android:duration="100" />
    <item android:drawable="@drawable/b" android:duration="100" />
    <item android:drawable="@drawable/c" android:duration="100" />
    <item android:drawable="@drawable/d" android:duration="100" />
    <item android:drawable="@drawable/e" android:duration="100" />
    <item android:drawable="@drawable/f" android:duration="100" />
    <item android:drawable="@drawable/g" android:duration="100" />
    <item android:drawable="@drawable/h" android:duration="100" />
    <item android:drawable="@drawable/i" android:duration="100" />
    <item android:drawable="@drawable/j" android:duration="100" />
</animation-list>

結構非常好懂:<animation-list> 是動畫的總標籤,裡面放若干個 <item> 影格節點,全部組合起來就是一段逐格動畫。

  • android:oneshot:非常關鍵的屬性,預設為 false 表示循環播放;設為 true 則只播放一次。
  • android:drawable:指定該影格使用的圖片資源。
  • android:duration:該影格停留的毫秒數,調這個值就能控制整體播放速度。

二、動手做個範例

接下來這個例子包含:播放與停止動畫、切換單次/循環播放、以及用滑桿調整透明度。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放動畫" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止動畫" />
    </LinearLayout>

    <RadioGroup android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/checkbox0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="單次播放" />

        <RadioButton
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="循環播放" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拖動進度條修改透明度(0 - 255)" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="256"
        android:progress="256"/>

    <ImageView
        android:id="@+id/imageView"
        android:background="@anim/animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

版面本身很單純,重點是最後那個 ImageView:透過 android:background="@anim/animation" 把它的背景設為一段動畫,資源位置就是剛才寫的 res/anim/animation.xml。當然,這行也可以在程式碼中設定:

imageView.setBackgroundResource(R.anim.animation);

接著用 getBackground() 就能把 AnimationDrawable 物件取出來:

/** 取得 ImageView 物件 */
imageView = (ImageView) findViewById(R.id.imageView);
/** 透過 ImageView 取得背景上的 AnimationDrawable */
animationDrawable = (AnimationDrawable) imageView.getBackground();

三、AnimationDrawable 的常用方法

這個類別就是控制逐格動畫的把手,提供了不少實用方法:

  • start():開始播放。
  • stop():停止播放。
  • setAlpha(int):設定透明度,取值範圍 0 – 255。
  • setOneShot(true):只播放一次。
  • setOneShot(false):循環播放。
  • isRunning():判斷是否正在播放。
  • getNumberOfFrames():取得總影格數。

四、完整程式碼

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SimpleActivity extends Activity {

    /** 播放動畫按鈕 */
    Button button0 = null;
    /** 停止動畫按鈕 */
    Button button1 = null;
    /** 播放模式單選鈕 */
    RadioButton radioButton0 = null;
    RadioButton radioButton1 = null;
    RadioGroup radioGroup = null;
    /** 用來調整透明度的滑桿 */
    SeekBar seekbar = null;
    /** 承載動畫的 View */
    ImageView imageView = null;
    /** 動畫物件 */
    AnimationDrawable animationDrawable = null;

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

        imageView = (ImageView) findViewById(R.id.imageView);
        animationDrawable = (AnimationDrawable) imageView.getBackground();

        // 播放動畫
        button0 = (Button) findViewById(R.id.button0);
        button0.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (!animationDrawable.isRunning()) {
                    animationDrawable.start();
                }
            }
        });

        // 停止動畫
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (animationDrawable.isRunning()) {
                    animationDrawable.stop();
                }
            }
        });

        radioButton0 = (RadioButton) findViewById(R.id.checkbox0);  // 單次播放
        radioButton1 = (RadioButton) findViewById(R.id.checkbox1);  // 循環播放
        radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkID) {
                if (checkID == radioButton0.getId()) {
                    animationDrawable.setOneShot(true);    // 單次播放
                } else if (checkID == radioButton1.getId()) {
                    animationDrawable.setOneShot(false);   // 循環播放
                }
                // 模式改變後讓動畫重新開始
                animationDrawable.stop();
                animationDrawable.start();
            }
        });

        // 用滑桿調整透明度
        seekbar = (SeekBar) findViewById(R.id.seekBar);
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                animationDrawable.setAlpha(progress);   // 設定透明度
                imageView.postInvalidate();             // 通知 UI 執行緒重繪
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }
}

這裡有一個最容易漏掉的關鍵:拖動滑桿調整 Alpha 之後,一定要呼叫 imageView.postInvalidate() 通知 UI 執行緒重繪,否則畫面上不會有任何變化。透明度改了卻沒重畫,看起來就像功能失效——這一點務必記牢。

五、實務上的兩點補充

第一,逐格動畫的影格圖片會全部載入記憶體,影格多、解析度高時很容易觸發 OutOfMemoryError。若動畫卡頓或閃退,第一步就是檢查每張影格的尺寸,並考慮改用較小的圖或縮減影格數。

第二,若在 Activity 尚未完全就緒時呼叫 start()(例如在 onCreate() 中直接播放),有可能沒有任何反應。較穩妥的做法是放在 onWindowFocusChanged() 或透過 View 的 post() 延後啟動,並在 onPause() 中 stop,避免背景還在耗電。

下載地址:https://vdisk.weibo.com/s/aalv0

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