Timeline
允许您动画属性以及在给定时间触发事件。在下面的示例中,矩形的rotate
属性使用2 KeyFrame
s为0到180之间的动画进行初始旋转和最终旋转值(第一个不需要,除非您想要倒转动画或播放它多于一次等等;在这种情况下需要更多的工作)。另外一个KeyFrame
触发EventHandler<ActionEvent>
这改变了Rectangle
的fill
:
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
transform到Node
和动画是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)),
...
谢谢! 但我需要围绕我的三角形中心旋转。我使用了RotateTransition,但它似乎不能在时间轴上工作? – Felix
@Felix它不能和'Timeline'一起工作,但是你可以动画'Node'的'Rotate'变换的'angle'属性。 – fabian
是的,它现在完美的工作! '旋转rt =新旋转(0,20,30,0,旋转.Y_AXIS)' – Felix