我在 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/