1、方法一:
? 利用CountDownTimer類
? //這個(gè)方法中的兩個(gè)參數(shù)第一個(gè)是 倒計(jì)時(shí)的時(shí)長(zhǎng),第二個(gè)是每次減少的時(shí)間
? ? CountDownTimer timer = new CountDownTimer(5000,1000) {
? ? @Override
? ? public void onTick(long millisUntilFinished) {
//donghua_textview展示數(shù)據(jù)的控件
? ? ? ? donghua_textview.setText(millisUntilFinished/1000+"S");
? ? }
//動(dòng)畫結(jié)束時(shí)調(diào)用的方法
? ? @Override
? ? public void onFinish() {
? ? ? ? startActivity(new Intent(DonghuaActivity.this, LoginActivity.class));
? ? }
};
//最后不要忘記調(diào)用start();方法
timer.start();
2、方法二:
? ? //利用Timer類
? ? int mSeconds = 5;
? ? Timer timer = new Timer();
? ? private Handler handler = new Handler() {
? ? ? ? @Override
? ? ? ? public void handleMessage(Message msg) {
? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? if (msg.what == 1) {
? ? ? ? ? ? ? ? if (mSeconds == 0) {
? ? ? ? ? ? ? ? ? ? timer.cancel();
? ? ? ? ? ? ? ? ? ? startActivity(new Intent(DonghuaActivity.this, LoginActivity.class));
? ? ? ? ? ? ? ? } else if (mSeconds > 0) {
? ? ? ? ? ? ? ? ? ? donghua_textview.setText(mSeconds + "S");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? };
? ? ? ? TimerTask task = new TimerTask() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? mSeconds--;
? ? ? ? ? ? ? ? handler.sendEmptyMessage(1);
? ? ? ? ? ? }
? ? ? ? };
//調(diào)用timer類中的schedule方法
? ? ? ? timer.schedule(task, 1000, 1000);
3、方法三:
? 利用handler發(fā)送延遲線程
? int mSeconds = 5;
? Handler handler =? new Handler();
? private void changeSeconds() {
? ? ? ? mSeconds--;
? ? ? ? handler.postDelayed(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? main_textview.setText(mSeconds+"S");
? ? ? ? ? ? ? ? if(mSeconds==0){
//在這里我們開啟動(dòng)畫不做跳轉(zhuǎn)
? ? ? ? ? ? ? ? ? ? initDonghua();
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? changeSeconds();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? },1000);
? ? }
//初始化動(dòng)畫
? private void initDonghua() {
? ? ? ? Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim);
? ? ? ? animation.setAnimationListener(this);
? ? ? ? main_textview.startAnimation(animation);
? ? }
? ? private void initView() {
? ? ? ? main_textview = (TextView) findViewById(R.id.main_textview);
? ? }
? ? @Override
? ? public void onAnimationStart(Animation animation) {
? ? }
? ? @Override
? ? public void onAnimationEnd(Animation animation) {
//在動(dòng)畫結(jié)束的時(shí)候進(jìn)行跳轉(zhuǎn)
? ? ? ? Intent intent = new Intent(MainActivity.this, SecondActivity.class);
? ? ? ? startActivity(intent);
? ? }
? ? @Override
? ? public void onAnimationRepeat(Animation animation) {
? ? }