Class Animation
Animation provides a high-level, object-oriented API for CSS @keyframes animations.
Each concrete subclass represents a specific visual effect (beat, bounce, spin, etc.) and
encapsulates its own @keyframes definition, timing function, and default configuration.
Developers configure animations using type-safe setters instead of raw CSS strings.
Speed Scale: The setSpeed(int) method accepts values 0-10,
where 0 is slowest (1200ms) and 10 is fastest (50ms). For precise control, use
setDuration(float) to override the speed scale with an exact duration in seconds.
Usage:
// Apply a spinning animation
icon.animate(new Spin());
// Bounce with custom speed
button.animate(new Bounce().setSpeed(7));
// Shake 3 times then stop
panel.animate(new Shake().setIterationCount(3));
// Stop any animation
icon.stopAnimation();
- Since:
- 2026
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAnimation(int defaultSpeed) Constructs an Animation with the specified default speed. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidapply(StyledElement<?> element) Applies this animation's CSS properties to the given style attributes.abstract KeyFramesCreates the@keyframesdefinition for this animation.protected abstract AnimationTimingFunctionReturns the default timing function for this animation.longReturns the animation duration in milliseconds.abstract StringReturns the unique ID for this animation's keyframes style element.Returns aCssStyleSheetcontaining this animation's keyframes.protected StringReturns the CSS timing function value as a string.setDelay(long ms) Sets the delay before the animation starts.setDirection(AnimationDirection direction) Sets the animation direction.setDuration(float seconds) Sets an exact duration in seconds, overriding the speed scale.setFillMode(AnimationFillMode fillMode) Sets the animation fill mode.setIterationCount(int count) Sets the number of times the animation plays.setSpeed(int speed) Sets the animation speed on a 0-10 scale.
-
Constructor Details
-
Animation
protected Animation(int defaultSpeed) Constructs an Animation with the specified default speed.- Parameters:
defaultSpeed- the default speed on a 0-10 scale
-
-
Method Details
-
setSpeed
Sets the animation speed on a 0-10 scale.Speed values map to durations: 0 = 1200ms (slowest), 5 = 300ms (default), 10 = 50ms (fastest). Values outside the 0-10 range are clamped.
- Parameters:
speed- the speed value (0-10)- Returns:
- this animation for method chaining
-
setDuration
Sets an exact duration in seconds, overriding the speed scale.Use this for precise timing control when the 0-10 speed scale is too coarse. For example,
setDuration(0.5f)sets a 500ms animation.- Parameters:
seconds- the duration in seconds- Returns:
- this animation for method chaining
-
setIterationCount
Sets the number of times the animation plays.Use -1 for infinite looping (the default for most animations). Use a positive integer for a specific number of repetitions.
- Parameters:
count- the iteration count (-1 for infinite)- Returns:
- this animation for method chaining
-
setDirection
Sets the animation direction.- Parameters:
direction- the direction (normal, reverse, alternate, alternate-reverse)- Returns:
- this animation for method chaining
-
setFillMode
Sets the animation fill mode.- Parameters:
fillMode- the fill mode (none, forwards, backwards, both)- Returns:
- this animation for method chaining
-
setDelay
Sets the delay before the animation starts.- Parameters:
ms- the delay in milliseconds- Returns:
- this animation for method chaining
-
getDurationMs
public long getDurationMs()Returns the animation duration in milliseconds.If a duration override has been set via
setDuration(float), that value is used. Otherwise, the duration is derived from the speed scale.- Returns:
- the duration in milliseconds
-
getKeyFramesId
Returns the unique ID for this animation's keyframes style element.This ID is used to deduplicate keyframe injection into the page head. Subclasses with configurable keyframes (e.g., Beat with a custom scale) should return a unique ID per configuration to avoid collisions.
- Returns:
- the keyframes style element ID
-
createKeyFrames
Creates the@keyframesdefinition for this animation.- Returns:
- a KeyFrames object defining the animation sequence
-
getDefaultTimingFunction
Returns the default timing function for this animation.Each animation subclass provides its own default timing function. For custom cubic-bezier or steps timing, override
getTimingFunctionValue()instead.- Returns:
- the default timing function, or null if
getTimingFunctionValue()provides a custom value
-
getTimingFunctionValue
Returns the CSS timing function value as a string.By default, delegates to
getDefaultTimingFunction(). Override this method to provide custom timing function values likecubic-bezier(...)orsteps(...)that are not represented by the enum.- Returns:
- the CSS timing function value
-
getStyleSheet
Returns aCssStyleSheetcontaining this animation's keyframes.Use this method for manual keyframe injection in
createHead()when the element is not yet attached to the page tree:head.addCssStyleSheet(animation.getStyleSheet());- Returns:
- a stylesheet containing the keyframes definition
-
apply
Applies this animation's CSS properties to the given style attributes.Sets
animation-name,animation-duration,animation-timing-function,animation-iteration-count,animation-direction,animation-fill-mode, andanimation-delayon the element.- Parameters:
element- the element to animate
-