作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 JavaFX 新手
我想让2个动画连续运行
然后我尝试创建 2 个圆圈,让第一个 (AQUA) 圆圈播放动画 1,然后让第二个 (RED) 圆圈播放动画 2。但如果只是这样,2 个动画将同时播放。
然后我尝试向其中添加 Thread.sleep():
import javafx.util.Duration;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class TestAnimation2 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
pane.setMinSize(1800, 900);
Group root = new Group(pane);
Scene mainScene = new Scene(root);
Circle cir1 = new Circle(100, 100, 30);
cir1.setFill(Color.AQUA);
pane.getChildren().add(cir1);
TranslateTransition animation1 = new TranslateTransition();
animation1.setToX(300);
animation1.setToY(0);
animation1.setDuration(Duration.seconds(2));
animation1.setNode(cir1);
Circle cir2 = new Circle(100, 200, 30);
cir2.setFill(Color.RED);
pane.getChildren().add(cir2);
TranslateTransition animation2 = new TranslateTransition();
animation2 = new TranslateTransition();
animation2.setToX(300);
animation2.setToY(0);
animation2.setDuration(Duration.seconds(2));
animation2.setNode(cir2);
primaryStage.setScene(mainScene);
primaryStage.setResizable(false);
primaryStage.show();
animation1.play();
try {
Thread.sleep(2000);
} catch (Exception e) {System.out.println(e);}
animation2.play();
}
public static void main(String[] args) {
launch();
}
}
但是当我尝试运行它时,我不明白为什么我看不到 animation1,程序停止,当它开始运行 animation2 时,它出现了。
如何让2个动画连续运行
最佳答案
您可以使用SequentialTransition
:
public class TestAnimation2 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
pane.setMinSize(1800, 900);
Group root = new Group(pane);
Scene mainScene = new Scene(root);
Circle cir1 = new Circle(100, 100, 30);
cir1.setFill(Color.AQUA);
pane.getChildren().add(cir1);
TranslateTransition animation1 = new TranslateTransition();
animation1.setToX(300);
animation1.setToY(0);
animation1.setDuration(Duration.seconds(2));
animation1.setNode(cir1);
Circle cir2 = new Circle(100, 200, 30);
cir2.setFill(Color.RED);
pane.getChildren().add(cir2);
TranslateTransition animation2 = new TranslateTransition();
animation2.setToX(300);
animation2.setToY(0);
animation2.setDuration(Duration.seconds(2));
animation2.setNode(cir2);
primaryStage.setScene(mainScene);
primaryStage.setResizable(false);
primaryStage.show();
SequentialTransition animation = new SequentialTransition(animation1, animation2);
animation.play();
}
public static void main(String[] args) {
launch();
}
}
或者,您可以使用第一个动画的 onFinished
处理程序来播放第二个:
public class TestAnimation2 extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
pane.setMinSize(1800, 900);
Group root = new Group(pane);
Scene mainScene = new Scene(root);
Circle cir1 = new Circle(100, 100, 30);
cir1.setFill(Color.AQUA);
pane.getChildren().add(cir1);
TranslateTransition animation1 = new TranslateTransition();
animation1.setToX(300);
animation1.setToY(0);
animation1.setDuration(Duration.seconds(2));
animation1.setNode(cir1);
Circle cir2 = new Circle(100, 200, 30);
cir2.setFill(Color.RED);
pane.getChildren().add(cir2);
TranslateTransition animation2 = new TranslateTransition();
animation2.setToX(300);
animation2.setToY(0);
animation2.setDuration(Duration.seconds(2));
animation2.setNode(cir2);
primaryStage.setScene(mainScene);
primaryStage.setResizable(false);
primaryStage.show();
animation1.setOnFinished(event -> animation2.play());
animation1.play();
}
public static void main(String[] args) {
launch();
}
}
关于java - 如何让2个动画连续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67742392/
我是一名优秀的程序员,十分优秀!