我有一些代码应该沿着圆弧的路径制作圆的动画:
package event_handling;
import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class PalindromeSwing extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
System.out.println(pane.getWidth());
Arc a = new Arc(100, 100, 100, 100, -135, 90);
a.setType(ArcType.OPEN);
a.setStroke(Color.BLACK);
a.setFill(Color.TRANSPARENT);
Circle c = new Circle(5);
pane.getChildren().addAll(a, c);
PathTransition pt = new PathTransition();
pt.setDuration(Duration.INDEFINITE);
pt.setNode(c);
pt.setPath(a);
pt.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setAutoReverse(true);
pt.play();
Scene scene = new Scene(pane, 400, 400);
primaryStage.setTitle("Animated circle");
primaryStage.setScene(scene);
primaryStage.show();
}
}
但是,当我运行该程序时,没有动画发生。圆出现在圆弧的开头,但没有任何反应:
请帮我理解原因。
最佳答案
您必须为动画设置明确的持续时间,例如
pt.setDuration(Duration.seconds(4));
该值决定一个动画周期的持续时间。
Duration.INDEFINITE
定义为 Duration(Double.POSITIVE_INFINITY)
。使用它将使动画播放无限持续时间,导致插值步长变得太小而无法对动画节点产生影响。
关于JavaFX PathTransition 动画未播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29993025/