Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 逐幀動畫實作:AnimationDrawable 上手

逐幀動畫的原理和電影膠片一樣:把若干張連續的圖按固定間隔輪流播放。Android 用 animation-list 描述這串影格,再由 AnimationDrawable 負責驅動,全程不必寫一行自定義繪製代碼。

一、定義動畫資源 res/anim/zhuzhen.xml

<?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/progress_1" android:duration="200"/>
    <item android:drawable="@drawable/progress_2" android:duration="200"/>
    <item android:drawable="@drawable/progress_3" android:duration="200"/>
    <item android:drawable="@drawable/progress_4" android:duration="200"/>
    <item android:drawable="@drawable/progress_5" android:duration="200"/>
    <item android:drawable="@drawable/progress_6" android:duration="200"/>
    <item android:drawable="@drawable/progress_7" android:duration="200"/>
    <item android:drawable="@drawable/progress_8" android:duration="200"/>
</animation-list>

兩個關鍵屬性:android:oneshot="false" 表示循環播放,設為 true 則只播一遍;android:duration 是該影格停留的毫秒數,這裡每格 200ms,八格剛好一輪 1.6 秒。

二、界面布局

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="開始"
        android:onClick="start"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止"
        android:onClick="stop"/>

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/zhuzhen"
        android:layout_gravity="center"/>
</LinearLayout>

三、MainActivity.java

package com.example.testanimation;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    private AnimationDrawable anim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView iv = (ImageView) findViewById(R.id.iv);
        anim = (AnimationDrawable) iv.getBackground();
    }

    public void start(View view) {
        anim.start();
    }

    public void stop(View view) {
        anim.stop();
    }
}

核心就一句:把 ImageView 的背景強制轉型成 AnimationDrawable,之後 start() 與 stop() 控制播放。因為動畫資源是設在 android:background 上,所以必須用 getBackground() 取;若改用 setImageDrawable 設定,就要相對改用 getDrawable()。

實務提醒

AnimationDrawable.start() 必須在 UI 執行緒呼叫,且不要在 onCreate 裡直接播放——此時視窗尚未完成掛載,動畫不會動。常見做法是在 onWindowFocusChanged() 中判斷 hasFocus() 為真後再啟動。影格尺寸應保持一致,否則播放時會出現跳動。

Copyright © Linux教程網 All Rights Reserved