Android 抽签动画

有人,不会选择;有人,不坚持选择;有人,不断地选择。

有时,选择比努力更重要;有时,没有重新选择的机会;有时,选择的结果是未知的。

如果人生路上遇到选择的小难题,抽一支签,看看天意,或许会有意外的惊喜!


抽签过程实质是帧动画的演示过程,需要用到AnimationDrawable,AnimationDrawable是实现Drawable Animations的基本类。推荐用XML文件的方法实现Drawable动画,不推荐在代码中实现。将新建的drawlots.xml文件(drawlots:抽签)放在res/anim文件夹下:

<?xml version="1.0" encoding="utf-8"?>  
<animation-list  xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">   
    <item android:drawable="@drawable/draw_lots_01" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_2" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_1" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_2" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_1" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_2" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_1" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_play_2" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_05" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_06" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_07" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_08" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_09" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_10" android:duration="100"/>  
    <item android:drawable="@drawable/draw_lots_11" android:duration="100"/>      
    <item android:drawable="@drawable/draw_lots_12" android:duration="100"/>      
    <item android:drawable="@drawable/draw_lots_13" android:duration="100"/>      
</animation-list>
XML文件中 <animation-list>元素为根节点, <item>节点定义了每一帧, duration是图片显示时间间隔。设置 android:oneshot为true,表示抽签动画只执行一次,设置为false则动画循环播放。下面是主要实现过程:

public class MainActivity extends Activity implements OnClickListener {

	ImageView img_draw; // 签
	Button btn_begin; // 开始抽签
	AnimationDrawable drawAnimation; // 抽签动画
	private int[] ids=new int[]{R.drawable.draw_lots_stop_01, 
			R.drawable.draw_lots_stop_02,R.drawable.draw_lots_stop_03}; //抽签结果
	private Random random; //产生抽签结果随机数
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView(); //初始化控件
	}
	
	/** 初始化控件  */
	private void initView() {
		img_draw = (ImageView) findViewById(R.id.img_draw);
		btn_begin = (Button) findViewById(R.id.drawlots_btn_begin);
		
		btn_begin.setOnClickListener(this);
		random=new Random();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.drawlots_btn_begin: //开始抽签
			try {
				img_draw.setBackgroundResource(R.anim.drawlots);
				drawAnimation = (AnimationDrawable) img_draw.getBackground();
				// 设置播放后重新启动
				drawAnimation.stop();  // 结束动画播放
				drawAnimation.start(); // 开始动画播放
				
			    int duration = 0; //抽签动画结束标志
                for (int i = 0; i < drawAnimation.getNumberOfFrames(); i++) {
                 duration += drawAnimation.getDuration(i);
                }
		        Handler handler = new Handler();
		        handler.postDelayed(new Runnable() {
		            public void run() {
		                //动画结束后换成结果图片
		            	int id=ids[random.nextInt(3)];
		            	img_draw.setBackgroundResource(id);
		            }
		        }, duration);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
	}
}
  一旦给指定的View设置Drawable Animation以后,其Background就变成了AnimationDrawable对象,即上面代码中的drawAnimation = (AnimationDrawable) img_draw.getBackground();

start()方法不能在onCreate()方法中调用,因为AnimationDrawable并未完全关联到Window;在onCreate()方法中,控件并未完全加载,因此获取到的控件宽高都为0。如果想在一开始就启动动画,应该在onWindowFocusChanged()方法中调用start(),控件的高度也可以在这个方法中获取。

抽签代码


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值