[英]Trigger event when reaches half of an PathTransition in JavaFX
I have a PathTransition to move a ImageView composed of 2 paths. 我有一个PathTransition来移动由2条路径组成的ImageView。
I want to add an action when the animation reaches half of its duration. 我想在动画达到其持续时间的一半时添加一个动作。
I saw that by the getCuePoints method, we could add markers at certain times but I don't know how link this marker at an action :/ 我通过getCuePoints方法看到了,我们可以在特定时间添加标记,但是我不知道如何在动作中链接该标记:/
Do you have an idea to do that? 你有个主意吗?
Thanks! 谢谢! :)
:)
Use a ParallelTransition
to run a PauseTransition
with a duration of half of that of the PathTransition
. 使用
ParallelTransition
运行PauseTransition
用了一半的持续时间PathTransition
。 This allows you to use the onFinished
handler to trigger the event: 这使您可以使用
onFinished
处理程序触发事件:
public void start(Stage primaryStage) {
Path path = new Path(new MoveTo(), new CubicCurveTo(100, 100, 150, 50, 200, 100));
Rectangle rect = new Rectangle(10, 10);
Rectangle back = new Rectangle(100, 50, Color.RED);
back.setVisible(false);
// original transition
PathTransition transition = new PathTransition(Duration.seconds(5), path, rect);
// transition for triggering halftime event
PauseTransition pause = new PauseTransition(transition.getCycleDuration().multiply(0.5));
pause.setOnFinished(evt -> back.setVisible(true));
// combine & play transitions
ParallelTransition animation = new ParallelTransition(rect, transition, pause);
animation.play();
Scene scene = new Scene(new Pane(back, rect), 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.