Image Frame Animation 多圖動畫

要在android的ImageView載入連續圖產生動化效果,我們需要先建一個animation-list的XML,名稱為anim1.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/anim1" 
    android:oneshot="false" 
    android:visible="true">

    <item android:drawable="@drawable/im1" android:duration="500"/>
    <item android:drawable="@drawable/im2" android:duration="500"/>
    <item android:drawable="@drawable/im3" android:duration="500"/>
    <item android:drawable="@drawable/im4" android:duration="500"/>
    <item android:drawable="@drawable/im5" android:duration="500"/>

</animation-list>
im1, im2...im5是指放在drawable中的連續圖。
android:oneshot->false是指無限循環, true是指不循環。
android:duration 的 500是 500ms的意思。


在layout   main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/iv_anim"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@anim/anim1" />

</RelativeLayout>


接下來onCreate中

public class MainActivity extends Activity{

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

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        if(hasFocus){
            ImageView ivAnim = (ImageView) findViewById(R.id.imageConfirmOrderGift);
            AnimationDrawable a1 = (AnimationDrawable) ivAnim.getDrawable();
            a1.start();
        }
    }
}
另外也可以
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/iv_anim"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@anim/anim1" />

</RelativeLayout>
但是onCreate 中就要改成

public class MainActivity extends Activity{

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

    @Override
    public void onWindowFocusChanged(boolean hasFocus){
        if(hasFocus){
            ImageView ivAnim = (ImageView) findViewById(R.id.imageConfirmOrderGift);
            ((AnimationDrawable) ivAnim.getBackground()).start();
            
        }
    }
}

留言

這個網誌中的熱門文章

python 找圖自動點擊

Python pyserial 抓取系統內的 COM PORT

VBA EXCEL 工作表變化 馬上執行 的作法 Worksheet_Change