android Frame-By-Frame Animations(一帧一帧地播放动画)的使用

来源:互联网 发布:js除法取整 编辑:程序博客网 时间:2024/05/16 08:01

程序功能,点击按钮时,图片一张一张循环播放:

直接来代码:

AppMain.java

package lxy.litsoft;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class AppMain extends Activity {//声明对象ImageView disPic;Button btDis;public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //实例化对象        disPic = (ImageView)findViewById(R.id.imageView01);        btDis = (Button)findViewById(R.id.button01);        //绑定监听器        btDis.setOnClickListener(new ButtonListener());    }//按钮的监听器实现class ButtonListener implements OnClickListener{public void onClick(View v) {//动画播放disPic.setBackgroundResource(R.drawable.anim);AnimationDrawable animationDrawable = (AnimationDrawable)disPic.getBackground();animationDrawable.start();}}}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"/>    <ImageView    android:id="@+id/imageView01"    android:layout_width="wrap_content"    android:layout_height="wrap_content"></ImageView>    <Button    android:id="@+id/button01"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="Anim"></Button></LinearLayout>

res/drawable/anim.xml

<animation-listxmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"><item android:drawable="@drawable/p30"android:duration="300"/><item android:drawable="@drawable/p31"android:duration="300"/><item android:drawable="@drawable/p32"android:duration="300"/><item android:drawable="@drawable/p33"android:duration="300"/><item android:drawable="@drawable/p34"android:duration="300"/><item android:drawable="@drawable/p35"android:duration="300"/><item android:drawable="@drawable/p36"android:duration="300"/><item android:drawable="@drawable/p37"android:duration="300"/></animation-list>

其中每一个item就是一帧的资源,android:drawable是图片资源,android:duration是每一帧的显示时间。