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

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

幀動畫的原理很直覺:把一張張連續的圖片依序快速切換,大腦就把它讀成動畫。Android 提供了 animation-list 這套宣告式做法,只要一份 XML 就能搞定播放順序與停留時間。

第一步:建立 res/anim/frame.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/girl_1"  android:duration="100" />
    <item android:drawable="@drawable/girl_2"  android:duration="100" />
    <item android:drawable="@drawable/girl_3"  android:duration="100" />
    <item android:drawable="@drawable/girl_4"  android:duration="100" />
    <item android:drawable="@drawable/girl_5"  android:duration="100" />
    <item android:drawable="@drawable/girl_6"  android:duration="100" />
    <item android:drawable="@drawable/girl_7"  android:duration="100" />
    <item android:drawable="@drawable/girl_8"  android:duration="100" />
    <item android:drawable="@drawable/girl_9"  android:duration="100" />
    <item android:drawable="@drawable/girl_10" android:duration="100" />
    <item android:drawable="@drawable/girl_11" android:duration="100" />
</animation-list>

這裡引用的是 drawable 底下的圖片,duration 是每一幀的停留時間,單位為毫秒,100 毫秒相當於每秒 10 幀。oneshot="false" 表示播放完會重複循環;改成 true 就只播一次。

第二步:版面 main.xml

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

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

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

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

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

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:background="@anim/frame" />

</LinearLayout>

要點整理

動畫掛在 ImageView 的 background 上,因此 Java 端要取得 AnimationDrawable 才能控制播放:

ImageView iv = (ImageView) findViewById(R.id.imageView1);
AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
ad.start();   // 開始播放
ad.stop();    // 停止播放

「單次/循環」可以透過 RadioGroup 切換 XML 裡的 oneshot 屬性(或直接重新載入不同動畫資源),SeekBar 則用 iv.setImageAlpha(progress) 或 getBackground().setAlpha() 即時調整透明度。幀動畫的好處是簡單直觀,缺點是每幀都是完整圖片、記憶體吃得多,幀數與解析度都要克制。

Copyright © Linux教程網 All Rights Reserved