围绕枢轴旋转平移?[英] RotateTransition around a pivot?

问题描述

我正在使用 RotateTransiton 旋转一条线,但它似乎通过线的中心旋转.我想用枢轴作为线的一端来旋转它.如何做到这一点?

推荐答案

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

如果要围绕不同的点旋转,请定义 Rotate transform,设置它的pivot,将它添加到行的变换列表,并使用 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);
    }
}

本文地址:https://www.itbaoku.cn/post/978348.html