码迷,mamicode.com
首页 > 移动开发 > 详细

Android 倒计时动画

时间:2014-11-07 19:10:37      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:android   动画   

今天给大家分享一个可以用来做倒计时的动画....下期开始准备写几篇动画的详细解释,毕竟授人以鱼不如授人以渔

接下来还是先上效果图


bubuko.com,布布扣


熟悉动画的人相信心中已经有了想法。。其实这个动画就是Alpha动画和Scale动画结合在一起

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

    <scale
        android:duration="800"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />

</set>

这是用set使俩个动画同时运行,如果想要依次的话,可以使用startOffset

	private TextView textView;
	private Animation animation;
	private Animation animation2;
	private int count = 30;
	private Button btnSwitch;
	private int flag = 0;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.text);
		animation = AnimationUtils.loadAnimation(this, R.anim.animation_text2);
		animation2 = AnimationUtils.loadAnimation(this, R.anim.animation_text);
		btnSwitch = (Button) findViewById(R.id.btn_switch);
		btnSwitch.setOnClickListener(this);
		textView.startAnimation(animation);
		handler.sendEmptyMessageDelayed(0, 1000);
	}

	public void small() {
		animation2.reset();
		textView.startAnimation(animation2);
	}

	public void big() {
		animation.reset();
		textView.startAnimation(animation);
	}

	private int getCount() {
		count--;
		if (count < 0 || count > 30) {
			count = 30;
		}
		return count;
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 0) {
				textView.setText("" + getCount());
				handler.sendEmptyMessageDelayed(0, 1000);
				small();
			} else {
				textView.setText("" + getCount());
				handler.sendEmptyMessageDelayed(1, 1000);
				big();
			}
		};
	};

	@Override
	public void onClick(View arg0) {
		switch (flag) {
		case 0:
			handler.removeMessages(0);
			handler.sendEmptyMessageDelayed(1, 1000);
			flag = 1;
			break;
		case 1:
			handler.removeMessages(1);
			handler.sendEmptyMessageDelayed(0, 1000);
			flag = 0;
			break;

		default:
			break;
		}

	}


项目源码

Android 倒计时动画

标签:android   动画   

原文地址:http://blog.csdn.net/u014163726/article/details/40895673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!