如何在javafx 2.1中等待转换结束? [英] How to wait for a transition to end in javafx 2.1?

查看:130
本文介绍了如何在javafx 2.1中等待转换结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景只包含 ImageView ,显示图片。我想将图像淡化为黑色(指定场景的颜色),然后一段时间后,再次从黑色渐变为图像。我发现 FadeTransition 非常适合此目的。这是我的一段代码:

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 the image again. I found the FadeTransition very fitting for this purpose. This is a piece of my code:

    // fade to black transition
    FadeTransition ft1 = new FadeTransition(Duration.millis(2000), myImageView);
    ft1.setFromValue(1.0);
    ft1.setToValue(0.0);
    ft1.play();

    // fade from black transition
    FadeTransition ft2 = new FadeTransition(Duration.millis(2000), myImageView);
    ft2.setFromValue(0.0);
    ft2.setToValue(1.0);
    ft2.play();

我的问题是 ft1.play()是异步的,因此下面的代码将在 ft1.play()退出之前开始执行。结果,我只看到第二个转换。如何等待第一个转换结束,然后启动第二个转换?我不能让线程之间的睡眠,因为它是主要的javafx线程(尝试和没有工作)。

My problem is that ft1.play() is asynchronous, so the code below will start being executed before ft1.play() is exited. As the result I see only the second transition. How can I wait for the first transition to end and then to launch the second transition? I cannot put the thread to sleep in between because it's the main javafx thread (tried and didn't work).

我尝试使用onFinishedProperty()方法与组合的一个忙等待在一个标志,但我被困在while循环永远。这是我的代码:

I tried using the onFinishedProperty() method with the combination of a busy-waiting on a flag, but I get stuck in the while loop forever. Here is my code for that:

    boolean isTransitionPlaying;
    FadeTransition ft = new FadeTransition(Duration.millis(2000), iv);
    ft.setFromValue(1.0);
    ft.setToValue(0.0);
    ft.onFinishedProperty().set(new EventHandler<ActionEvent>() {
        @Override 
        public void handle(ActionEvent actionEvent) {
            transitionPlaying = false;
        }
    });
    transitionPlaying = true;
    ft.play();

    while (transitionPlaying == true)
    {
        // busy wait
        System.out.println("still waiting...");
    }

    FadeTransition ft2 = new FadeTransition(Duration.millis(2000), iv);
    ft2.setFromValue(0.0);
    ft2.setToValue(1.0);
    ft2.play();

如何正确等待?谢谢

推荐答案

JavaFX应用程序线程上的忙等待(或甚至Thread.sleep)总是一个坏主意 - 处理UI处理的线程,所以你的过渡,以及你的UI的其余部分,永远不会更新 - 有效地冻结你的应用程序UI在忙等待期间。对于响应式UI,您需要尽快在FX应用程序线程上运行您的逻辑,然后让线程运行,因此其余的JavaFX系统可以处理它。这是为什么过渡有异步回调 - 一旦你习惯了它们,是一种非常自然的开发方式。

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.

除了Uluk的解决方案),您还可以查看 SequentialTransition 类在顺序执行转换中的处理辅助。注意,如果你想在SequentialTransition完成后采取一个动作,你仍然希望添加一个onFinished处理程序到SequentialTransition以采取行动。

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.

这篇关于如何在javafx 2.1中等待转换结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆