【问题标题】:SequentialTransition .stop() doesn't wait until the transition end its cycleSequentialTransition .stop() 不会等到过渡结束其周期
【发布时间】:2013-12-17 02:32:43
【问题描述】:

我在 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();
    }
}

【问题讨论】:

    标签: java javafx javafx-2


    【解决方案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();
    }}
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 2018-10-15
      相关资源
      最近更新 更多