首页 > 代码库 > Android 倒计时动画

Android 倒计时动画

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

接下来还是先上效果图




熟悉动画的人相信心中已经有了想法。。其实这个动画就是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 倒计时动画