2016-04-21 49 views
0

我在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

一种可能性是将动画节点的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();