Android用Animation-list實現逐幀動畫


先看看效果圖

下面是2個動畫的xml文件

animation1.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
 根標簽為animation-list,其中oneshot代表着是否只展示一遍,設置為false會不停的循環播放動畫        
 根標簽下,通過item標簽對動畫中的每一個圖片進行聲明        android:duration 表示展示所用的該圖片的時間長度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/icon1"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon2"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon3"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon4"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon5"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon6"
        android:duration="250">
    </item>

</animation-list>

animation2.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
 根標簽為animation-list,其中oneshot代表着是否只展示一遍,設置為false會不停的循環播放動畫        
 根標簽下,通過item標簽對動畫中的每一個圖片進行聲明        android:duration 表示展示所用的該圖片的時間長度
-->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true" >

    <item
        android:drawable="@drawable/icon6"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon5"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon4"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon3"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon2"
        android:duration="250">
    </item>
    <item
        android:drawable="@drawable/icon1"
        android:duration="250">
    </item>

</animation-list>

xml布局文件:

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

    <ImageView
        android:id="@+id/animationIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:src="@anim/animation1" 
        android:scaleType="center"/>

    <Button
        android:id="@+id/buttonA"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:onClick="onClick"
        android:text="順序顯示" />

    <Button
        android:id="@+id/buttonB"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:onClick="onClick"
        android:text="停止" />

    <Button
        android:id="@+id/buttonC"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:onClick="onClick"
        android:text="倒序顯示" />

</LinearLayout>

java代碼:

package com.example.animationdemo;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private ImageView animationIV;
    private AnimationDrawable animationDrawable;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        animationIV = (ImageView) findViewById(R.id.animationIV);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.buttonA:
            start_animation(R.anim.animation1);
            break;
        case R.id.buttonB:
            stop_animation();
            break;
        case R.id.buttonC:
            start_animation(R.anim.animation2);
            break;
        default:
            break;
        }
    }
    private void start_animation(int id){
        animationIV.setImageResource(id);
        animationDrawable = (AnimationDrawable) animationIV
                .getDrawable();
        animationDrawable.start();
    }
    private void stop_animation(){
        animationDrawable.stop();
    }
}

 csdn下載地址:http://download.csdn.net/detail/wenwei19861106/4856995

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM