Georgian
Georgian

Reputation: 8960

JavaFX Smooth Animation

So I have this Timeline custom animation. Node transformation is irrelevant.

final KeyValue kv1 = new KeyValue(angle, HALF_PI,  Interpolator.LINEAR);
final KeyValue kv2 = new KeyValue(angle, -HALF_PI, Interpolator.EASE_BOTH);

final KeyFrame kf1 = new KeyFrame(Duration.millis(0),   kv1);
final KeyFrame kf2 = new KeyFrame(Duration.millis(500), kv2);

Timeline animation = new Timeline(kf1, kf2);

animation.setRate(1);

My angle variable is modified in this timeline and used in the transformation of a node. As you can see, in this case the rate is 1, and the duration is 500.

To smoothen out the transformation, I'm guessing that the angle should go through several discrete values, which are set by the Interpolator.

Also, I'm not inclined to use a Task to achieve this. Only if the lagginess is severly less than if using the Timeline.

Question:

Which adjustment smoothens out the animation?

A. Low rate + High duration
B. High rate + Low duration
C. ??? Other

Upvotes: 1

Views: 3876

Answers (1)

James_D
James_D

Reputation: 209225

The rate is not the frame rate. None of these settings will affect the smoothness of the animation. Using a Task won't help: the Animation API is just a high-level mechanism to update properties of your UI on each frame render, in a way that interpolates values with respect to time. If you put work in a Task, you'll have to update the properties on the FX Application Thread anyway, so you're just replicating the code that's provided for you by the Animation API.

Behind the scenes, JavaFX has a thread which renders frames to the graphical system. The target frame rate is (currently) 60fps. At each frame render, the rendering thread has to synchronize with the JavaFX Application thread, so if you're doing too much work on that thread the frame rate will be below 60fps.

I think Animations work as follows: At each frame render, any running animations will update their associated properties by interpolation along the time line between key frames. The basic algorithm is pretty obvious: look at the current time, subtract the start time, divide by the difference between the end and start times, use that value to update the property.

All the rate does is act as a multiplier for the interpolator. So if you set rate=-1, the animation runs backwards. If you set rate=2, it completes in half the specified duration. If you set rate =-0.5, it completes in twice the specified duration, and runs backwards, etc. The frame rate is still going to be whatever the underlying FX implementation can squeeze out of the system, up to a maximum (in the current implementation) of 60 fps.

Smoothness is basically affected by frame rate (again, the only control you have over this is to make sure you're not flooding the FX Application thread with too much work), along with how much motion you get per frame. Very fast animations will involve something moving considerable distances per frame, and will look less smooth than something that is moving a barely-perceptible distance per frame.

Upvotes: 5

Related Questions