JavaFX:窗口/阶段更改期间的动画

问题描述 投票:0回答:1

如何在更改舞台的过程中制作动画?

-例如,我有两个具有不同大小(宽度和高度)的舞台。通常,没有任何动画,第二个屏幕会立即弹出。我希望第二阶段能够顺利显示(例如在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();

任何与我要查找的内容相关的线程都将受到赞赏。

animation javafx scenebuilder
1个回答
0
投票

尝试使用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();

由于您可以使用插值器,因此趋于平滑

© www.soinside.com 2019 - 2024. All rights reserved.