Animation JavaFX:窗口/舞台更改期间的动画

Animation JavaFX:窗口/舞台更改期间的动画,animation,javafx,scenebuilder,Animation,Javafx,Scenebuilder,如何在阶段变化期间创建动画 -例如,我有两个不同大小(宽度和高度)的阶段。通常,没有任何动画,第二个屏幕会在瞬间弹出。我希望第二阶段能够顺利进行(比如在Mac OS Sierra中打开或关闭窗口)。我能找到的所有教程都是关于变化场景中的动画的。也许我的搜索方式是错误的 以下是我如何改变阶段 FXMLLoader loader = new FXMLLoader(getClass().getResource(Insert FXML here)); Parent root = loader

如何在阶段变化期间创建动画

-例如,我有两个不同大小(宽度和高度)的阶段。通常,没有任何动画,第二个屏幕会在瞬间弹出。我希望第二阶段能够顺利进行(比如在Mac OS Sierra中打开或关闭窗口)。我能找到的所有教程都是关于变化场景中的动画的。也许我的搜索方式是错误的

以下是我如何改变阶段

  FXMLLoader loader = new FXMLLoader(getClass().getResource(Insert FXML here));
    Parent root = loader.load();
    Stage stage = new Stage();
    stage.setResizable(false);
    stage.initStyle(StageStyle.UNDECORATED);
    stage.setScene(new Scene(root));
    stage.show();
非常感谢与我所寻找的内容相关的任何线索

试试AnimationTimer。 时间线不起作用,因为阶段的属性是只读的


FXMLLoader loader = new FXMLLoader(getClass().getResource(Insert FXML here));
Parent root = loader.load();
Stage stage = new Stage();

stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(new Scene(root));
stage.show();


//set your Stage width,height,x,y to zero

stage.setX(0);
stage.setY(0);
stage.setWidth(0);
stage.setHeight(0);
AnimationTimer t = new AnimationTimer() {
            @Override
            public void handle(long now) {
                stage.setX(stage.getX()+1);
                stage.setX(stage.getX()+1);
                stage.setWidth(stage.getWidth()+1);
                stage.setHeight(stage.getHeight()+1);
                // Here put your condition for "destination and desired size" 
                 // in this case everything will move/grow by 600 px
                if( stage.getX() == 600){
                    this.stop();
                    stage.setResizable(false);
                }
            }
        };
        t.start();

这看起来不够平滑,但您可以找到一种“插值”机制,使运动更加平滑

编辑

我忘了!您还可以使用转换:

Animation transition = new Transition() {
    {
        setCycleDuration(Duration.millis(2000));
        setInterpolator(Interpolator.EASE_IN);
    }
    @Override
    protected void interpolate(double frac) {
        // frac is the percentage of how much time of the Transition
        // Already passed (1000 ms => frac = 0.5)
        // Here you just Multiply frac with the designated value
        stage.setHeight(height*frac);
        stage.setWidth(width*frac);
        stage.setX(x*frac);
        stage.setY(y*frac);        
    }
};
transition.play();
由于您可以使用插值器尝试使用AnimationTimer,因此这会更加平滑。 时间线不起作用,因为阶段的属性是只读的


FXMLLoader loader = new FXMLLoader(getClass().getResource(Insert FXML here));
Parent root = loader.load();
Stage stage = new Stage();

stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(new Scene(root));
stage.show();


//set your Stage width,height,x,y to zero

stage.setX(0);
stage.setY(0);
stage.setWidth(0);
stage.setHeight(0);
AnimationTimer t = new AnimationTimer() {
            @Override
            public void handle(long now) {
                stage.setX(stage.getX()+1);
                stage.setX(stage.getX()+1);
                stage.setWidth(stage.getWidth()+1);
                stage.setHeight(stage.getHeight()+1);
                // Here put your condition for "destination and desired size" 
                 // in this case everything will move/grow by 600 px
                if( stage.getX() == 600){
                    this.stop();
                    stage.setResizable(false);
                }
            }
        };
        t.start();

这看起来不够平滑,但您可以找到一种“插值”机制,使运动更加平滑

编辑

我忘了!您还可以使用转换:

Animation transition = new Transition() {
    {
        setCycleDuration(Duration.millis(2000));
        setInterpolator(Interpolator.EASE_IN);
    }
    @Override
    protected void interpolate(double frac) {
        // frac is the percentage of how much time of the Transition
        // Already passed (1000 ms => frac = 0.5)
        // Here you just Multiply frac with the designated value
        stage.setHeight(height*frac);
        stage.setWidth(width*frac);
        stage.setX(x*frac);
        stage.setY(y*frac);        
    }
};
transition.play();

由于您可以使用插值器,这会变得更加平滑。我编辑了我的答案,因为我忘记了您也可以使用过渡进行平滑插值。请注意,您仍然可以使用时间线。窗口的x、y、width和height属性可能都是只读的,但它们仍然具有设置器,您可以将其封装在
可写值
@Luxusproblem中。第二个设置器工作得更好。但是如何使动画从中心开始呢?两者都从左上角开始。@VayneMain_345您可以使用中间部分,而不是将阶段的值设置为零。但在这种情况下,您需要了解如何从中心而不是从“原点”应用插值。当你想让舞台一直在中央时,把窗口设置在你屏幕的中央。当你插值时,你需要计算x和y:比如:
stage.setX(centerX-width*frac/2)
stage.setY(centerX-height*frac/2)
。但是JavaFX的渲染器在“调整舞台大小”方面会有问题。它不会像你转换一个节点那样平滑。@VayneMain_345我编辑了我的答案,因为我忘了你也可以使用转换进行平滑插值。注意,你仍然可以使用
时间线
。窗口的x、y、width和height属性可能都是只读的,但它们仍然具有设置器,您可以将其封装在
可写值
@Luxusproblem中。第二个设置器工作得更好。但是如何使动画从中心开始呢?两者都从左上角开始。@VayneMain_345您可以使用中间部分,而不是将阶段的值设置为零。但在这种情况下,您需要了解如何从中心而不是从“原点”应用插值。当你想让舞台一直在中央时,把窗口设置在你屏幕的中央。当你插值时,你需要计算x和y:比如:
stage.setX(centerX-width*frac/2)
stage.setY(centerX-height*frac/2)
。但是JavaFX的渲染器在“调整舞台大小”方面会有问题。它不会像转换节点那样平滑。