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

Android 幀動畫實戰:用 AnimationDrawable 實現逐幀播放

編輯:關於 Android 程式設計

幀動畫的原理很樸素:把多張連續的圖片按固定時間間隔快速切換,利用視覺殘留形成動畫效果。它特別適合播放序列幀素材,比如加載中的小人、開屏動畫、表情特效等。

先看最終效果:

第一步:準備素材

用 Android Studio 新建一個工程,把幀動畫每一幀要用的圖片依次放進 drawable 文件夾。命名建議有規律一些(g1、g2、g3……),後面配置起來省事:

第二步:編寫 animation-list 描述文件

在 drawable 下新建 frameanimation.xml,用 animation-list 標簽把每一幀和它的顯示時長聲明出來:

<?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/g1"  android:duration="200" />
    <item android:drawable="@drawable/g2"  android:duration="200" />
    <item android:drawable="@drawable/g3"  android:duration="200" />
    <item android:drawable="@drawable/g4"  android:duration="200" />
    <item android:drawable="@drawable/g5"  android:duration="200" />
    <item android:drawable="@drawable/g6"  android:duration="200" />
    <item android:drawable="@drawable/g7"  android:duration="200" />
    <item android:drawable="@drawable/g8"  android:duration="200" />
    <item android:drawable="@drawable/g9"  android:duration="200" />
    <item android:drawable="@drawable/g10" android:duration="200" />
    <item android:drawable="@drawable/g11" android:duration="200" />
</animation-list>

兩個關鍵屬性:android:duration 表示該幀停留的毫秒數,數值越小動畫越快;android:oneshot 設為 true 表示只播放一遍,false 則是無限循環。

第三步:修改布局文件

在 activity_main.xml 中放一個 ImageView 作為載體:

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

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

第四步:在 Activity 中啟動動畫

package com.fyt.frameanimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 獲得布局文件上的 ImageView 控件
        ImageView iv = (ImageView) findViewById(R.id.iv);

        // 把幀動畫的資源文件指定為 ImageView 的背景
        iv.setBackgroundResource(R.drawable.frameanimation);

        // 獲取 ImageView 的背景,轉型為 AnimationDrawable
        AnimationDrawable ad = (AnimationDrawable) iv.getBackground();

        // 播放幀動畫
        ad.start();
    }
}

幾個實踐建議

· 幀動畫一定要掛在 onCreate 或 onResume 之後再 start,如果在 onCreate 之前調用, often 會出現沒有動畫的情況;
· 幀數不宜過多、單幀不宜過大,否則內存峰值會很高,容易 OOM。素材建議按屏幕密度做壓縮處理;
· 記得在 onPause 中調用 ad.stop(),避免離開頁面後仍在後台消耗資源;
· 如果只需要播放一次,把 oneshot 設為 true,並在 AnimationDrawable 的 selectDrawable 或計時器邏輯中處理結束後的收尾工作。

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