如何调整动画中按钮的大小javafx



我在javafx中使用一个动画,所以当我点击按钮时,它会随着动画移动,但我也想调整它的大小(它应该越来越小)。

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        /*Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));*/
        AnchorPane root=new AnchorPane();
        Timeline timeline = new Timeline();
        Button button = new Button();

        timeline.getKeyFrames().addAll(
                new KeyFrame(Duration.ZERO,
                        new KeyValue(button.translateXProperty(), 500),
                        new KeyValue(button.translateYProperty(), 500)),
                new KeyFrame(Duration.seconds(.5), // set end position at 40s
                        new KeyValue(button.translateXProperty(), 200),
                        new KeyValue(button.translateYProperty(), 200)));

        timeline.play();

        root.getChildren().addAll(button);
        primaryStage.show();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 800, 800));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

一种可能性是动画化节点的缩放属性:

timeline.getKeyFrames().addAll(
                                       new KeyFrame(Duration.ZERO,
                                                    new KeyValue(button.translateXProperty(), 500),
                                                    new KeyValue(button.translateYProperty(), 500),
                                                    new KeyValue(button.scaleXProperty(), 1),
                                                    new KeyValue(button.scaleYProperty(), 1)),
                                       new KeyFrame(Duration.seconds(.5), // set end position at 40s
                                                    new KeyValue(button.translateXProperty(), 200),
                                                    new KeyValue(button.translateYProperty(), 200),
                                                    new KeyValue(button.scaleXProperty(), .4),
                                                    new KeyValue(button.scaleYProperty(), .4)));
        timeline.play();   

最新更新