How to wait for a transition to end in javafx 2.1?

后端 未结 3 1666
心在旅途
心在旅途 2020-12-16 05:07

My scene consists only of an ImageView, displaying an image. I would like to fade the image to black (assigned color of the scene), then after some time, fade from black to

相关标签:
3条回答
  • 2020-12-16 05:09

    I had problem where other code was doing some calculations and I wanted to run Animations in JavaFX app, but needed to make other code to wait for animation to finish. I wasn't able to tell this other code when animation has finished, so I have created method for playing Animation and then waiting for it to finish:

        private synchronized void playAnimationAndWaitForFinish(final Animation animation) {
        if (Platform.isFxApplicationThread()) {
            throw new IllegalThreadStateException("Cannot be executed on main JavaFX thread");
        }
        final Thread currentThread = Thread.currentThread();
        final EventHandler<ActionEvent> originalOnFinished = animation.getOnFinished();
        animation.setOnFinished(new EventHandler<ActionEvent>() {
    
            @Override
            public void handle(ActionEvent event) {
                if (originalOnFinished != null) {
                    originalOnFinished.handle(event);
                }
                synchronized (currentThread) {
                    currentThread.notify();
                }
            }
        });
        Platform.runLater(new Runnable() {
    
            @Override
            public void run() {
                animation.play();
            }
        });
        synchronized (currentThread) {
            try {
                currentThread.wait();
            } catch (InterruptedException ex) {
                //somebody interrupted me, OK
            }
        }
    }
    

    It is required that this method is not invoked in main JavaFX thread, otherwise, it works for me.

    0 讨论(0)
  • 2020-12-16 05:10

    Ok if your ft2 is the reflective animation of ft1 then do

    ft1.setAutoReverse(true);
    ft1.setCycleCount(1);
    // Or
    // ft1.setCycleCount(Timeline.INDEFINITE);
    // to loop infinitely (blinking effect) until stop()
    

    and you don't ft2. If you still need ft2 to play after ft1 then

    ft1.setOnFinished(new EventHandler<ActionEvent>() {
    
        @Override
        public void handle(ActionEvent event) {
            ft2.play();
        }
    });
    
    0 讨论(0)
  • 2020-12-16 05:26

    Busy waiting (or even Thread.sleep) on the JavaFX application thread is always a bad idea - you tie up the thread which handles the UI processing so your transitions, as well as the rest of your UI, is never updated - effectively freezing your app UI for the duration of the busy wait. For a responsive UI, you need to run your logic on the FX application thread as quickly as possible, then let the thread go so the rest of the JavaFX system can get on with it's processing. This is why the transitions have async callbacks - which, once you get used to them, are a very natural way of developing.

    In addition to Uluk's solutions (which are great), you could also look at the SequentialTransition class for handling assistance in performing transitions in sequence. Note that if you want to take an action after the SequentialTransition has completed, you will still want to add an onFinished handler to the SequentialTransition to take action at that time.

    0 讨论(0)
提交回复
热议问题