RotateTransition围绕一个支点?

我使用RotateTransiton来旋转一条线,但它似乎在线的中心旋转。 我想用枢轴旋转它作为线的一端。 这个怎么做?

RotateTransition通过更改rotate属性来工作 ,正如您所观察到的那样,它定义了围绕Node中心的旋转。

如果要围绕不同的点Rotate ,请定义Rotate变换 ,设置其轴,将其添加到线的变换列表 ,并使用Timeline来操纵其角度。

这是一个例子:

 import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; public class RotateLineAboutEnd extends Application { @Override public void start(Stage primaryStage) { Line line = new Line(200, 200, 200, 350); Pane pane = new Pane(line); Rotate rotation = new Rotate(); rotation.pivotXProperty().bind(line.startXProperty()); rotation.pivotYProperty().bind(line.startYProperty()); line.getTransforms().add(rotation); Timeline timeline = new Timeline( new KeyFrame(Duration.ZERO, new KeyValue(rotation.angleProperty(), 0)), new KeyFrame(Duration.seconds(1), new KeyValue(rotation.angleProperty(), 360))); Button button = new Button("Rotate"); button.setOnAction(evt -> timeline.play()); button.disableProperty().bind(timeline.statusProperty().isEqualTo(Animation.Status.RUNNING)); HBox controls = new HBox(button); controls.setAlignment(Pos.CENTER); controls.setPadding(new Insets(12)); BorderPane root = new BorderPane(pane, null, null, controls, null); Scene scene = new Scene(root, 400, 400); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 

(在JavaFX 2.2和JavaFX 8上)在我看来,最好的方法是转换节点的layoutBounds(包含枢轴点)并以相反的方式转换节点本身。

一个例子:

 public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { Group root = new Group(); primaryStage.setScene(new Scene(root, 140, 140)); Rectangle rect = new Rectangle(1, 1, 40, 40); // comment movePivot to get the default rotation movePivot(rect, -20, -20); RotateTransition rt = new RotateTransition(Duration.seconds(4),rect); rt.setToAngle(720); rt.setCycleCount(Timeline.INDEFINITE); rt.setAutoReverse(true); rt.play(); primaryStage.show(); } // this is the function you want private void movePivot(Node node, double x, double y){ node.getTransforms().add(new Translate(-x,-y)); node.setTranslateX(x); node.setTranslateY(y); } public static void main(String[] args) { launch(args); } }