@Immutable @Utility public final class PropertySetter extends java.lang.Object
TimingTarget
instances that enables automating
the animation of object properties. The returned TimingTarget
instances from the getTarget or getTargetTo static factory
methods should be added as a timing target of an animation. The timing events
from the animation will change the specified property over time.
For example, here is an animation of the "background" property of some object
obj
from blue to red over a period of one second:
TimingTarget ps = PropertySetter.getTarget(obj, "background", Color.BLUE, Color.RED); Animator animator = new AnimatorBuilder().setDuration(1, TimeUnit.SECONDS).addTarget(ps).build(); animator.start();More complex animations can be created by passing in multiple values for the property to take on, for example:
TimingTarget ps = PropertySetter.getTarget(obj, "background", Color.BLUE, Color.RED, Color.GREEN); Animator animator = new AnimatorBuilder().setDuration(1, TimeUnit.SECONDS).addTarget(ps).build(); animator.start();It is also possible to define more involved and tightly-controlled steps in the animation, including the times between the values and how the values are interpolated by using the constructor that takes a
KeyFrames
object.
KeyFrames
defines the fractional times at which an object takes on
specific values, the values to assume at those times, and the method of
interpolation between those values. For example, here is the same animation
as above, specified through KeyFrames, where the RED color will be set 10% of
the way through the animation (note that we are not setting an Interpolator,
so the timing intervals will use the default LinearInterpolator):
KeyFramesBuilder<Color> builder = new KeyFramesBuilder<Color>(Color.BLUE); builder.setFrame(Color.RED, 0.1); builder.setFrame(Color.GREEN, 1); KeyFrames<Color> frames = builder.build(); TimingTarget ps = PropertySetter.getTarget(obj, "background", frames); Animator animator = new AnimatorBuilder().setDuration(1, TimeUnit.SECONDS).addTarget(ps).build(); animator.start();It is also possible to setup a
PropertySetter
to use the current
value of the property as the starting value in an animation. This is called a
"to" animation. For example, here is an animation of the "foreground"
property of some object obj
from it's current value to white and then
to blue over a period of 5 seconds:
TimingTarget ps = PropertySetter.getTargetTo(obj, "foreground", Color.WHITE, Color.BLUE); Animator animator = new AnimatorBuilder().setDuration(5, TimeUnit.SECONDS).addTarget(ps).build(); animator.start();Note the use of getTargetTo, rather than getTarget, to construct an instance for a "to" animation.
As with getTarget, it is also possible with getTargetTo to
define more involved and tightly-controlled steps in the animation, including
the times between the values and how the values are interpolated by using the
constructor that takes a KeyFrames
object. In the case of a "to"
animation, the first key value of the KeyFrames
object is
ignored—it is replaced with the current value of the property. For
example, here is the same animation as above, specified through a list of
KeyFrames.Frame
objects, where the WHITE color will be set 40% of the
way through the animation. The final transition to BLUE uses a
SplineInterpolator
rather than the default LinearInterpolator
. The RED color that is specified as the starting value is ignored and
replaced with the current value of the foreground property.
KeyFramesBuilder<Color> builder = new KeyFramesBuilder<Color>(Color.RED); builder.setFrame(Color.WHITE, 0.4); builder.setFrame(Color.BLUE, 1, new SplineInterpolator(0.00, 1.00, 1.00, 1.00)); KeyFrames<Color> frames = builder.build(); TimingTarget ps = PropertySetter.getTargetTo(obj, "foreground", frames); Animator animator = new AnimatorBuilder().setDuration(5, TimeUnit.SECONDS).addTarget(ps).build(); animator.start();
All the methods in this utility return a TimingTargetAdapter
so that
a "debug" name can be explicitly set on a returned timing target. In the
example code below calling ps.getDebugName()
will result in "BlueToRed" and calling
ps.toString()
will result in
"PropertySetterTimingTarget@BlueToRed".
TimingTargetAdapter ps = PropertySetter.getTarget(obj, "background", Color.BLUE, Color.RED); ps.setDebugName("BlueToRed");
If the target class has more than one setter method defined due to method overloading (e.g., we want setX and declarations exist for setX(int) and setX(double)) then the following heuristic, in order, determines the method chosen. Implementation note: the key value types are internally always wrapper types (e.g., Integer never int) so primitive type setter methods must be considered.
The "debug" name is automatically set to the value passed for the property
name. In the example code below the TimingTargetAdapter
type doesn't
need to be used but calling ps.toString()
will result in
"PropertySetterTimingTarget@background".
TimingTarget ps = PropertySetter.getTarget(obj, "background", Color.BLUE, Color.RED);
Modifier and Type | Method and Description |
---|---|
static <T> TimingTargetAdapter |
getTarget(java.lang.Object object,
java.lang.String propertyName,
Interpolator interpolator,
T... values)
Constructs a timing target that changes an object's property over time.
|
static <T> TimingTargetAdapter |
getTarget(java.lang.Object object,
java.lang.String propertyName,
KeyFrames<T> keyFrames)
Constructs a timing target that changes an object's property over time.
|
static <T> TimingTargetAdapter |
getTarget(java.lang.Object object,
java.lang.String propertyName,
T... values)
Constructs a timing target that changes an object's property over time.
|
static <T> TimingTargetAdapter |
getTargetTo(java.lang.Object object,
java.lang.String propertyName,
Interpolator interpolator,
T... values)
Constructs a timing target that changes an object's property from its
current value over time.
|
static <T> TimingTargetAdapter |
getTargetTo(java.lang.Object object,
java.lang.String propertyName,
KeyFrames<T> keyFrames)
Constructs a timing target that changes an object's property from its
current value over time.
|
static <T> TimingTargetAdapter |
getTargetTo(java.lang.Object object,
java.lang.String propertyName,
T... values)
Constructs a timing target that changes an object's property from its
current value over time.
|
public static <T> TimingTargetAdapter getTarget(java.lang.Object object, java.lang.String propertyName, KeyFrames<T> keyFrames)
T
- the type of the object's property.object
- an object.propertyName
- the name of the the property to manipulate on object.keyFrames
- a key frames instance that define how the property's value changes
over time.public static <T> TimingTargetAdapter getTarget(java.lang.Object object, java.lang.String propertyName, T... values)
T
- the type of the object's property.object
- an object.propertyName
- the name of the the property to manipulate on object.values
- an ordered list of values that the property should be animated
between. The values will be spaced equally in time and a
LinearInterpolator
will be used.public static <T> TimingTargetAdapter getTarget(java.lang.Object object, java.lang.String propertyName, Interpolator interpolator, T... values)
T
- the type of the object's property.object
- an object.propertyName
- the name of the the property to manipulate on object.interpolator
- the interpolator that should be used between the values.values
- an ordered list of values that the property should be animated
between. The values will be spaced equally in time and the passed
interpolator will be used.public static <T> TimingTargetAdapter getTargetTo(java.lang.Object object, java.lang.String propertyName, KeyFrames<T> keyFrames)
T
- the type of the object's property.object
- an object.propertyName
- the name of the the property to manipulate on object.keyFrames
- a key frames instance that define how the property's value changes
over time. The initial value is ignored and replaced with the
current value of the object's property.public static <T> TimingTargetAdapter getTargetTo(java.lang.Object object, java.lang.String propertyName, T... values)
T
- the type of the object's property.object
- an object.propertyName
- the name of the the property to manipulate on object.values
- an ordered list of values that the property should be animated
between. The current value of the object's property is added to
the start of this ordered list. The values will be spaced equally
in time and a LinearInterpolator
will be used.public static <T> TimingTargetAdapter getTargetTo(java.lang.Object object, java.lang.String propertyName, Interpolator interpolator, T... values)
T
- the type of the object's property.object
- an object.propertyName
- the name of the the property to manipulate on object.interpolator
- the interpolator that should be used between the values.values
- an ordered list of values that the property should be animated
between. The current value of the object's property is added to
the start of this ordered list. The values will be spaced equally
in time and the passed interpolator will be used.