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

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

直接来代码:

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-list
	xmlns: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是每一帧的显示时间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值