繁体   English   中英

加快计时器速度?

[英]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;
}

从Java文档中:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM