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

时间:2016-04-21 00:17:32

标签: 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);
    }
}

1 个答案:

答案 0 :(得分:1)

一种可能性是为节点的scaleProperties设置动画:

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();