Using the Interpolator API
By default, Timeline
calculates how property values change between KeyFrame
objects using a linear function—changing property values proportionally to the time passed.
You can control that more precisely using the Interpolator API.
Predefined interpolators
You can set interpolators when creating KeyValue
:
new KeyValue(node.translateYProperty(), 200, Interpolator.LINEAR);
The default interpolator is Interpolator.LINEAR
.
Visually, it looks like an abrupt stop at the end of the animation. To soften that, you can use one of the easing interpolators:
Interpolator.EASE_IN
: Starts slowlyInterpolator.EASE_OUT
: Ends slowlyInterpolator.EASE_BOTH
: Starts and ends slowly
You can see them all at work simultaneously by running the following code:
// chapter5/basics/InterpolatorsDemo.java public void start(Stage primaryStage) { VBox root = new VBox(10); Timeline timeline = new Timeline(); timeline.setCycleCount(Timeline.INDEFINITE); timeline.setAutoReverse...