In the last entry we used code like this to start an animation when a button was clicked.
button.addActionListener(new ActionListener() {The Timing Framework provides triggers which extend the basic functionality of the framework. The framework provides a number of trigger classes which can trigger animations.
public void actionPerformed(ActionEvent e) {
Animator anim = new Animator(2000, 5,
Animator.RepeatBehavior.REVERSE, new ButtonTimingTarget());
anim.setAcceleration(0.4f);
anim.setDeceleration(0.4f);
anim.start();
}
});
We can simplify the above code using an action trigger which corresponds to ActionEvents and so is perfect to use with a button. The trigger may be instantiated directly by client code but of more convenience is the static addTrigger method which also takes the object on which the trigger should operate.
For example, to add an action trigger to the button we'd do the following:
Animator anim = new Animator(2000, 5,This may not seem like a huge reduction but it removes the boilerplate of the action listener implementation and removes the need for the anonymous inner class making the code arguably much more readable.
Animator.RepeatBehavior.REVERSE, new ButtonTimingTarget());
anim.setAcceleration(0.4f);
anim.setDeceleration(0.4f);
ActionTrigger.addTrigger(button, anim);
Currently the framework provides four trigger implementations:
- ActionTrigger - as shown above, may be used on any component that fires action events.
- FocusTrigger - may be used on components that fire focus events such as text fields
- MouseTrigger - may be used on components that fire mouse events (pretty much every component) such as when the mouse enters or leaves a component
- TimingTrigger - allows sequencing of different animations, for example starting an animation when another animation ends
Next time I'll take this a step further and use a property setter to simplify the code even more.
No comments:
Post a Comment