Android在其他动作重复中重复动作
对不起我的英语!:) 好的,我想每秒重复几次,就像这里:Android在其他动作重复中重复动作,android,Android,对不起我的英语!:) 好的,我想每秒重复几次,就像这里: //Declare the timer Timer t = new Timer(); //Set the schedule function and rate t.scheduleAtFixedRate(new TimerTask() { @Override public void run() { //Called each time when 1000 milliseconds (1 secon
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period parameter)
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
现在,在它里面,我想生成1-3(包括)之间的随机数,如果它是3,那么做一些事情
因此:
在if语句中,我想重复其他操作(每隔5毫秒移动ImageView或类似的操作)。我怎么做?谢谢。您可以使用倒计时 e、 g.创建30秒(30000ms)的倒计时,并每秒(1000ms)通知 或者只是一个处理者。 对于动画,您应该了解ObjectAnimator/ViewPropertyAnimation
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Random rand = new Random();
int num = rand.nextInt(3)+1;
if(num==3){
// repeat action here.
}
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//I get called every 1000ms
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
//dostuff
handler.postDelayed(this,1000); //repeat after a second
}
});