SequentialTransition .stop()不会等到转换结束它的周期

时间:2013-12-17 02:32:44

标签: 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();
    }
}

1 个答案:

答案 0 :(得分:1)

解决问题的方法很棘手,你必须拿一些标志来检查是否需要完成交易或继续交易。在我的代码中,我使用了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();
}}