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

Android AnimationDrawable:用 XML 做幀動畫的完整說明

編輯:關於 Android 程式設計

Drawable animation 是一張接一張切換 Drawable 資源來形成動畫的技術,AnimationDrawable 就是它的核心類別。官方建議以 XML 宣告的方式實作,而不是在程式碼中動態組裝——XML 版本結構清楚、修改方便,也更容易看出整體的播放順序。

一、動畫定義檔

把 XML 放在 res/drawable/ 目錄下,以 <animation-list> 作為根節點,每個 <item> 就是一幀,分別指定要顯示的 drawable 與持續時間(毫秒):

<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="true">

    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />

</animation-list>

android:oneshot 決定播放行為:設為 true 時動畫只跑一遍並停在最後一幀;設為 false 則會無限循環。

二、兩種掛載方式

方式一:AnimationDrawable 本身就是 Drawable,可直接在 XML 版面中指定為某個 View 的背景,然後呼叫 start()。

方式二:在程式碼中用 View.setBackgroundResource(resID) 掛上,再呼叫 start()。

三、基本範例

AnimationDrawable rocketAnimation;

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

    ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
    // rocket_thrust 就是我們定義的 animation-list
    rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
    rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}

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

一旦為某個 View 設定了 Drawable Animation,它的背景就會變成 AnimationDrawable 物件,因此可以用 getBackground() 轉型取回來操作。

四、最容易踩到的坑:start() 不能寫在 onCreate()

原因在於 AnimationDrawable 此時尚未完全關聯到 Window,View 還沒真正完成顯示,動畫自然起不來。同樣的道理,在 onCreate() 中量測某個 View 的寬高往往只得到 0;SurfaceHolder 需要額外掛 Callback,也是同一個根源。

解法是把啟動時機往後挪,最推薦放在 onWindowFocusChanged() 中——這是能保證 View 已完成配置的最早時機。

五、順帶驗證:View 尺寸為何是 0

用一個簡單的例子就能確認上述推論。版面檔中放一個固定尺寸的 TextView:

<TextView
    android:id="@+id/textView"
    android:layout_width="50dip"
    android:layout_height="100dip"
    android:text="@string/special_character" />

然後在 onWindowFocusChanged() 中讀取它的實際尺寸:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    int width  = mTextView.getMeasuredWidth();
    int height = mTextView.getMeasuredHeight();

    Log.d("window_focus", "textview width is:" + width);
    Log.d("window_focus", "textview height is:" + height);
}

在這個時機點就能拿到正確的 50dip 與 100dip 換算後的值。結論很清楚:只有在 View 完全關聯到 Window 之後,才能取得它的寬高,也才能可靠地為它設定背景。

六、進階:結合 Alpha 動畫與監聽器

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        imageView.setBackgroundResource(R.anim.frame_animation);
        animDrawable = (AnimationDrawable) imageView.getBackground();
        animDrawable.start();

        AlphaAnimation aas = new AlphaAnimation(0.1f, 1.0f);
        aas.setDuration(3500);
        imageView.startAnimation(aas);

        aas.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                imageView.setBackgroundResource(R.anim.frame_animation);
                animDrawable = (AnimationDrawable) imageView.getBackground();
                animDrawable.setAlpha(80);
                animDrawable.start();
            }

            @Override
            public void onAnimationRepeat(Animation animation) { }

            @Override
            public void onAnimationEnd(Animation arg0) {
                // 淡入結束後收起圖片並停止幀動畫
                imageView.setVisibility(View.GONE);
                animDrawable.stop();
            }
        });
    }
}

這個組合示範了兩件事:幀動畫(AnimationDrawable)與補間動畫(AlphaAnimation)可以並行運作,前者負責內容變化、後者負責呈現效果;而透過 AnimationListener 你能精準掌握動畫的生命週期,在結束時做收尾——例如隱藏元件並停止幀動畫,避免它持續消耗資源。

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