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

Android 逐幀動畫:用 animation-list 播放連續圖片

編輯:Android 開發實例

先前看到用 UIImageView 播放連續圖片的做法,就想到 Android 上同樣能做到——本質上就是幾張圖片反覆切換。這種播放事先備好的圖像序列、模擬電影膠卷效果的方式,學名叫 frame by frame animation。

實作步驟

步驟一:建立動畫描述檔

在 res 目錄下建一個 anim 資料夾,新增 picture_animation.xml。(這個檔案不一定要放在 anim 底下,放 drawable 也可以;我這裡刻意分開,是為了區分「圖片效果用的 xml」與「補間動畫用的 xml」。)

<?xml version="1.0" encoding="utf-8"?>
<!-- 動畫幀集合物件 -->
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <!-- android:duration 表示每一幀顯示的毫秒數。
         放在 drawable 下的動畫圖片不能太大,否則記憶體會爆掉 -->
    <item android:drawable="@drawable/camp_fire1"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire2"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire3"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire4"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire5"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire6"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire7"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire8"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire9"  android:duration="80" />
    <item android:drawable="@drawable/camp_fire10" android:duration="80" />
    <item android:drawable="@drawable/camp_fire11" android:duration="80" />
    <item android:drawable="@drawable/camp_fire12" android:duration="80" />
    <item android:drawable="@drawable/camp_fire13" android:duration="80" />
    <item android:drawable="@drawable/camp_fire14" android:duration="80" />
    <item android:drawable="@drawable/camp_fire15" android:duration="80" />
    <item android:drawable="@drawable/camp_fire16" android:duration="80" />
    <item android:drawable="@drawable/camp_fire17" android:duration="80" />
</animation-list>

兩個屬性要說明:android:oneshot="false" 代表播放到最後一幀後會回到第一幀持續循環,設為 true 則只播一次;android:duration 的單位是毫秒,80ms 相當於每秒約 12.5 幀。

步驟二:在版面配置中放進 ImageView

<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="start"
    android:id="@+id/start" />

<ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageId"
    android:src="@anim/picture_animation" />

關鍵是 android:src 指向剛剛那個 animation-list 資源。

步驟三:在 Activity 中控制播放

private AnimationDrawable draw = null;

// 取得 ImageView,並把它背後的 Drawable 轉型為 AnimationDrawable
this.imageView = (ImageView) findViewById(R.id.imageId);
this.draw = (AnimationDrawable) imageView.getDrawable();

this.start.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        startAnimation();
    }
});

private void startAnimation() {
    if (draw.isRunning()) {
        draw.stop();
        start.setText("start");
    } else {
        draw.stop();      // 先 stop 一次,確保從第一幀重新開始
        draw.start();
        start.setText("pause");
    }
}

兩個實務上會踩到的細節

AnimationDrawable 必須在畫面已經附加到視窗之後才能 start。如果在 onCreate() 裡直接呼叫 start(),動畫不會動。這也是範例把播放控制放在按鈕點擊(或 onWindowFocusChanged)裡的原因。

幀圖是全部載入記憶體的。17 張全螢幕大圖足以讓應用 OOM。務必把幀圖裁到實際顯示尺寸,並控制總張數;張數多、每張又大的情況,應該改用 SurfaceView 自行管理繪製,或考慮 spritesheet 的方式。

原始碼:https://henzil.googlecode.com/svn/trunk/donghua/

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