2016-08-19 86 views
0

我需要让动作在动画的一半时发生。像我的矩形在Y轴上旋转180度。我需要它在90度完成时更改颜色,并在完成180度时使文本可见。旋转矩形上的时间轴

我被告知使用时间轴并添加事件处理程序到相关的关键帧,但我真的不知道如何。

public void open() { 
    RotateTransition trans = new RotateTransition(Duration.seconds(1), rec); 
    trans.setOnFinished(event -> { 
    text.setVisible(true); 
    rec.setFill(Color.WHITE); 
    }); 
    trans.play(); 
} 

回答

1

Timeline允许您动画属性以及在给定时间触发事件。在下面的示例中,矩形的rotate属性使用2 KeyFrame s为0到180之间的动画进行初始旋转和最终旋转值(第一个不需要,除非您想要倒转动画或播放它多于一次等等;在这种情况下需要更多的工作)。另外一个KeyFrame触发EventHandler<ActionEvent>这改变了Rectanglefill

Rectangle rec = new Rectangle(50, 50, 50, 50); 

Duration rotateDuration = Duration.seconds(5); 
Timeline timeline = new Timeline(
     new KeyFrame(Duration.ZERO, new KeyValue(rec.rotateProperty(), 0)), // initial rotate 
     new KeyFrame(rotateDuration.divide(2), evt -> { 
      // event for halfway through 
      rec.setFill(Color.RED); 
     }), 
     new KeyFrame(rotateDuration, new KeyValue(rec.rotateProperty(), 180)) // end value of rotate 
); 

timeline.play(); 

如果需要指定一个支点,你也可以申请一个Rotate transformNode和动画是angle属性:

Rotate rotate = new Rotate(0, pivotX, pivotY); 
rec.getTransforms().add(rotate); 

... 
Timeline timeline = new Timeline(
     new KeyFrame(Duration.ZERO, new KeyValue(rotate.angleProperty(), 0)), 
     ... 
+0

谢谢! 但我需要围绕我的三角形中心旋转。我使用了RotateTransition,但它似乎不能在时间轴上工作? – Felix

+0

@Felix它不能和'Timeline'一起工作,但是你可以动画'Node'的'Rotate'变换的'angle'属性。 – fabian

+0

是的,它现在完美的工作! '旋转rt =新旋转(0,20,30,0,旋转.Y_AXIS)' – Felix