java - SequentialTransition .stop() 不会等到过渡结束其循环

标签 java javafx javafx-2

我在 javafx 中进行了顺序转换以在两个循环之间淡入/淡出。

我的问题是当我为顺序转换调用 .stop() 时,它停止了转换,但它不会等待转换直到它完成它的转换周期,所以它停止了转换有时在褪色过程的一半!我如何让它在褪色周期完成后停止顺序转换?所以当我回想起 .play().playfromStart() 时,它能再次正确播放吗?

我的代码示例:

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Transitions extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Rectangle circle1 = RectangleBuilder.create()
                .arcHeight(300)
                .arcWidth(300)
                .height(30)
                .width(40)
                .opacity(0.6)
                .translateY(30)
                .style("-fx-fill: linear-gradient(#82d24f,#398907); -fx-stroke: #4a4a4a; -fx-stroke-width: 2")
                .build();

        Rectangle circle2 = RectangleBuilder.create()
                .arcHeight(300)
                .arcWidth(300)
                .height(40)
                .width(50)
                .opacity(0.7)
                .translateX(10)
                .translateY(70)
                .style("-fx-fill: linear-gradient(#82d24f,#398907); -fx-stroke: #4a4a4a; -fx-stroke-width: 2.5")
                .build();

        FadeTransition fade1 = FadeTransitionBuilder.create()
                .duration(Duration.millis(200))
                .node(circle1)
                .toValue(0.3)
                .cycleCount(2)
                .autoReverse(true)
                .build();

        FadeTransition fade2 = FadeTransitionBuilder.create()
                .duration(Duration.millis(100))
                .node(circle2)
                .toValue(0.3)
                .cycleCount(2)
                .autoReverse(true)
                .build();

        SequentialTransition ChildBalloonFade = SequentialTransitionBuilder.create()
                .children(fade1,fade2)
                .cycleCount(Timeline.INDEFINITE)
                .build();

        stage.setScene(new Scene(new Group(circle1, circle2)));
        stage.show();
        ChildBalloonFade .play();
    }
}

最佳答案

这里有一些技巧可以解决您的问题,您必须使用一些标志来检查是否需要完成交易或继续交易。在我的代码中,我为此使用了 ToggleButton,

public class Transitions extends Application {
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    Rectangle circle1 = RectangleBuilder.create()
            .arcHeight(300)
            .arcWidth(300)
            .height(30)
            .width(40)
            .opacity(0.6)
            .translateY(30)
            .style("-fx-fill: linear-gradient(#82d24f,#398907); -fx-stroke: #4a4a4a; -fx-stroke-width: 2")
            .build();

    Rectangle circle2 = RectangleBuilder.create()
            .arcHeight(300)
            .arcWidth(300)
            .height(40)
            .width(50)
            .opacity(0.7)
            .translateX(10)
            .translateY(70)
            .style("-fx-fill: linear-gradient(#82d24f,#398907); -fx-stroke: #4a4a4a; -fx-stroke-width: 2.5")
            .build();

    FadeTransition fade1 = FadeTransitionBuilder.create()
            .duration(Duration.millis(200))
            .node(circle1)
            .toValue(0.3)
            .cycleCount(2)
            .autoReverse(true)
            .build();

    FadeTransition fade2 = FadeTransitionBuilder.create()
            .duration(Duration.millis(100))
            .node(circle2)
            .toValue(0.3)
            .cycleCount(2)
            .autoReverse(true)
            .build();

    SequentialTransition ChildBalloonFade = SequentialTransitionBuilder.create()
            .children(fade1,fade2)
            .cycleCount(Timeline.INDEFINITE)
            .build();

    final ToggleButton button = new ToggleButton("Stop");

    fade2.setOnFinished(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            if (button.isSelected()) {
                ChildBalloonFade.stop();
            }
        }
    });

    button.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            if (!button.isSelected()) {
                ChildBalloonFade.play();
            }
        }
    });

    VBox vBox = new VBox();
    vBox.getChildren().addAll(circle1, circle2, button);
    stage.setScene(new Scene(vBox));
    stage.show();
    ChildBalloonFade.play();
}}

关于java - SequentialTransition .stop() 不会等到过渡结束其循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20624867/

相关文章:

java - 如何在使用新生成的实体标识符时深度复制 Hibernate 实体

JavaFX - Mac 断言失败

java - 在JavaFX Controller 类中添加向initialize()抛出异常的方法

java - 是否可以使用 Bindings API 将 ObservableList 的非空状态绑定(bind)到 ObjectProperty 中?

java - 在 Swing 中创建可重新排列的选项卡

java - 场景构建器不显示 Controller 字段

java - NoClassDefFoundError。如何设置依赖关系并部署java应用程序?

java - Jackson - 将 JsonNode 转换为 xml

java - Spring 4.x OSGI 支持

java - 当 JavaFX 中的整个 Pane 发生变化时,我应该有各种 FXML 文件吗?