如何改变AnimationTimer的速度?

Aly*_*ona 5 java animation javafx timer

AnimationTimer用于几个任务,如动画,更改图片和ProgressIndicator动画.为了达到所需的速度,我让线程进入睡眠状态,但是当几个动画同时运行时,它们会影响彼此的速度.有没有其他方法来改变速度AnimationTimer?代码示例:

private void initialize() {
 programButtonAnimation=new AnimationTimer(){
            @Override
            public void handle(long now) {
                    showClockAnimation();
            }
        };
 programButtonAnimation.start();
}

private void showClockAnimation(){
    String imageName = "%s_"+"%05d"+".%s";
    String picturePath="t093760/diploma/view/styles/images/pink_frames/"+String.format( imageName,"pink" ,frameCount,"png");
    programButton.setStyle("-fx-background-image:url('"+picturePath+"')");
    frameCount++;
    try {
        Thread.sleep(28);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if(frameCount>=120){
        programButtonAnimation.stop();
        frameCount=0;
    }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*s_D 6

所述AnimationTimerhandle方法对于被呈现,在FX应用程序线程的每个帧调用一次.你永远不应该阻止该线程,所以不要Thread.sleep(...)在这里打电话.

传递给handle(...)方法的参数是时间戳,以纳秒为单位.因此,如果您想要限制更新,以便它们不会发生多次,例如28毫秒,您可以使用它来执行此操作:

private void initialize() {
 programButtonAnimation=new AnimationTimer(){

            private long lastUpdate = 0 ;
            @Override
            public void handle(long now) {
                    if (now - lastUpdate >= 28_000_000) {
                        showClockAnimation();
                        lastUpdate = now ;
                    }
            }
        };
 programButtonAnimation.start();
}

private void showClockAnimation(){
    String imageName = "%s_"+"%05d"+".%s";
    String picturePath="t093760/diploma/view/styles/images/pink_frames/"+String.format( imageName,"pink" ,frameCount,"png");
    programButton.setStyle("-fx-background-image:url('"+picturePath+"')");
    frameCount++;
    if(frameCount>=120){
        programButtonAnimation.stop();
        frameCount=0;
    }
}
Run Code Online (Sandbox Code Playgroud)


Rol*_*and 5

由于我已经编写了代码并且 James_D 更快地通知您您阻止了 UI,我仍然想补充一点,如果您有多个不同时间的 AnimationTimer,您应该为此创建一个专用类。如果它们中的每一个都以不同的速度运行,您可以像这样实现它:

import javafx.animation.AnimationTimer;

public abstract class AnimationTimerExt extends AnimationTimer {

    private long sleepNs = 0;

    long prevTime = 0;

    public AnimationTimerExt( long sleepMs) {
        this.sleepNs = sleepMs * 1_000_000;
    }

    @Override
    public void handle(long now) {

         // some delay
        if ((now - prevTime) < sleepNs) {
            return;
        }

        prevTime = now;

        handle();
    }

    public abstract void handle();

}
Run Code Online (Sandbox Code Playgroud)

这意味着至少在经过 sleepMs 毫秒后调用 handle() 方法。

或者您可以更改参数并指定 fps,无论您需要什么。

您可以像这样使用上面的代码:

AnimationTimerExt timer = new AnimationTimerExt(100) {

    @Override
    public void handle() {

        System.out.println( System.currentTimeMillis());

    }
};

timer.start();
Run Code Online (Sandbox Code Playgroud)

此外,一遍又一遍地加载图片也不是最好的选择。如果您想做动画,我建议您查看 Mike 的关于使用 JavaFX 创建 Sprite 动画的博客。