1://timer定时器(在子线程中发送消息,在主线程通过handler接收消息)
private TextView tv;
private TimerTask timerTask;
private Timer timer;
int a=5;
String[] b={"一","二","三","四","五"};
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
a--;
if (a>=0){
tv.setText(b[a]);
}else {
timer.cancel();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
tv = (TextView) findViewById(R.id.tv);
timerTask = new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
};
timer = new Timer();
timer.schedule(timerTask,1000,1000);
}
2:使用handler.postdelayed() (在多长时间后,启动runnable线程)
private TextView tv;
private int time = 3;
private Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
tv.setText(time + "s");
time--;
if (time == 0) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
} else {
tv.setText(time + "s");
initDjs();
}
}
}, 1000); (这里的1000就是每间隔一秒变一次)
3:使用 CountDownTimer 实现倒计时
CountDownTimer timer = new CountDownTimer(3000, 1000) {//第一个参数表示总时间,第二个参数表示间隔时间
@Override
public void onTick(long millisUntilFinished) {
tv.setText("倒计时"+millisUntilFinished/1000+"s");
}
//结束时
@Override
public void onFinish() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}.start();
4:动画倒计时
//跳转 这是给动画监听的方法
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
} //动画开始
@Override
public void onAnimationEnd(Animation animation) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
} //动画结束
@Override
public void onAnimationRepeat(Animation animation) {
} //动画重复
});