Java Reference
In-Depth Information
Creating custom interpolators for animation
The recipe Building animation with the KeyFrame API introduced the notion of keyframe
animation using Interpolator instances to automatically calculate the in-between
values between starting and ending keyframes. As of version 1.2, JavaFX comes with
five interpolators including Interpolator.EASEIN , Interpolator.EASEOUT ,
Interpolator.BOTH , Interpolator.LINEAR , and Interpolator.DISCRETE
(see Building animation with the KeyFrame API for details).
While these interpolators are adequate for most animation sequences, you may, however,
encounter situations where you want your objects to behave differently than the ways offered
by the built-in interpolators. In this recipe, we will show you how to create your own custom
Interpolator class.
Getting ready
This recipe uses keyframe animation concepts supported by the Timeline and KeyFrame
classes to create animation sequences. If you are not familiar with keyframe-based animation
in JavaFX, review recipe Building animation with the KeyFrame API . This recipe will define a
new interpolator class by extending base class SimpleInterceptor . If you are not familiar
with defining and creating classes in JavaFX, please review Chapter 1 , Declaring and using
JavaFX classes .
How to do it...
To illustrate how to create your own custom interpolator, the recipe presents a simple
interpolator class called MagneticInterpolator . As the name suggests, when using this
interpolator, interpolated values briskly snap to the end value when a certain threshold (the
attraction value) has been reached. You can access the full listing of the abbreviated code
shown next in source-code/src/animation/MagneticInterpolatorDemo.fx .
class MagneticInterpolator extends SimpleInterpolator {
public var attraction:Number = 0.05;
override public function curve (t : Number) : Number {
if(t >= 1 - attraction){
}else if(t if(t attraction){
Search WWH ::

Custom Search