[英]Speeding up a timer?
我試圖使我的當前計數器/計時器的速度在整數變小時加快。 例如,它將是10 ......... 9 ........,8 ........,7 ....... 6 ..... 。5 ..... 4 .... 3 ... 2 .. 1。
當前代碼:
private int interval;
private Timer timer;
public void startTimer(final Player p, String seconds) {
String secs = seconds;
int delay = 1000;
int period = 1000;
timer = new Timer();
interval = Integer.parseInt(secs);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
p.sendMessage("" + setInterval(p));
}
}, delay, period);
}
private final int setInterval(Player p) {
if (interval == 1){
timer.cancel();
p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Countdown Finished!");
}
return --interval;
}
public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
從指定的時間開始計划指定的任務以重復執行固定速率 。 隨后的執行大約每隔固定的時間間隔執行一次,並間隔指定的時間。
使用此方法,您無法更改周期,因為此方法不是為此設計的。 如果您嘗試訪問任務中的句點,則編譯器將失敗,因為在任務中看不到句點。
如果您不希望將線程,可運行對象和wait()方法纏住,並希望堅持使用Timer類中的方法(我假設您曾經使用過-但為了記錄起見, 請在您發布的文章中添加導入內容)如果您使用的是源代碼,如果您使用的是另一個可能包含文檔的軟件包的方法 ,請考慮使用public void schedule(TimerTask task, long delay)
,並包裝在while循環中。 然后,您可以在循環中更改delay參數,並計划在不同時間段內執行任務。
當然,您必須弄清楚倒計時完成后如何退出while循環(一種“快速又臟”的方式將使用布爾值)。 但是由於您基本上在while循環的每次迭代中“重置”了計時器,所以timer.cancel()不會為您做任何事情。 取消一次,但是下一個迭代計時器將簡單地重新啟動任務。
一種選擇是使用javafx.animation.Timeline
。
例如 :
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CountDown extends Application {
private static final double SPEED_UP_FACTOR = 0.15; // + 15%
@Override
public void start(Stage primaryStage) throws Exception {
IntegerProperty counter = new SimpleIntegerProperty(10);
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(counter.get()), new KeyValue(counter, 0))
);
counter.addListener((observable, oldValue, newValue) -> {
double currentRate = timeline.getRate();
timeline.setRate(currentRate + currentRate * SPEED_UP_FACTOR); // speed up count down
System.out.println(newValue);
});
timeline.setOnFinished(event -> {
System.out.println("CountDown Finished!");
Platform.exit();
});
timeline.play();
}
public static void main(String[] args) {
launch(args);
}
}
編輯 :
或簡單地使用Thread.sleep()
:
public class CountDown {
private static final double SPEED_UP_FACTOR = 0.15; // + 15%
public static void main(String[] args) {
CountDownTimer timer = new CountDownTimer(10, 1000);
new Thread(timer).start();
}
static final class CountDownTimer implements Runnable {
final int initialValue;
final long intervalMillis;
CountDownTimer(int initialValue, long intervalMillis) {
this.initialValue = initialValue;
this.intervalMillis = intervalMillis;
}
@Override
public void run() {
int counter = initialValue;
double rate = 1;
while (counter > 0) {
sleep(Math.round(intervalMillis / rate));
rate += rate * SPEED_UP_FACTOR;
System.out.println(--counter);
}
}
}
private static void sleep(long timeout) {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.