Skip to content

StyleAnimation

Since 2.2.1

StyleAnimation is a runtime style animator for UIElement.
Use it when you want to animate style properties directly in code (for example on click, hover, or custom events), instead of relying only on passive transition.


Usage

UIElement.animation() automatically selects the current element as the animation target.

java
target.animation()
        .duration(1f)
        .ease(Eases.QUAD_IN_OUT)
        .style(PropertyRegistry.TRANSFORM_2D, new Transform2D().scale(0.5f).translate(100f, 0))
        .style(PropertyRegistry.OPACITY, 0f)
        .onFinished(element -> {
            target.animation()
                    .ease(Eases.QUART_IN_OUT)
                    .style(PropertyRegistry.TRANSFORM_2D, new Transform2D())
                    .style(PropertyRegistry.OPACITY, 1f)
                    .start();
        })
        .start();

Tips

WARNING

StyleAnimation.start() only works when ModularUI is valid (non-null).

If you want an enter animation (play once when the element is mounted into a valid ModularUI), use the callback overload:

java
target.animation(anim -> {
    anim.duration(0.5f)
        .ease(Eases.QUART_OUT)
        .style(PropertyRegistry.OPACITY, 1f)
        .start();
});

The callback is invoked immediately when ModularUI is already available, or once after UIEvents.MUI_CHANGED when it becomes available.

kotlin
target.animationDsl {
    duration(0.5f)
    ease(Eases.QUART_OUT)
    style(PropertyRegistry.OPACITY, 1f)
}

Kotlin DSL does not usually need extra start() handling:

  • animationDsl { ... } defaults to start = true
  • animation execution is deferred until ModularUI becomes valid

Keyframes

style(property, frames...) supports keyframes by progress (0.0 -> 1.0).

java
target.animation()
        .duration(2f)
        .ease(Eases.QUAD_IN_OUT)
        .style(PropertyRegistry.OPACITY,
                FloatObjectPair.of(0.5f, 0f))
        .start();

Target Selection

For multi-target animation, create an animation from ModularUI and select by element or selector.

java
StyleAnimation.of(mui)
        .select(targetElement)
        .select(".card:hover")
        .lss("opacity", 0.5f)
        .start();

Lifecycle and Control

start() returns ISubscription.
Call unsubscribe() to stop/cancel the running animation subscription.

You can also configure:

  • duration(float)
  • delay(float)
  • ease(IEase)
  • onInterpolate((runtime, element) -> ...)
  • onFinished(element -> ...)
  • origin(StyleOrigin) and animationOrigin(StyleOrigin) for advanced style-origin control

API Reference

MethodReturnsDescription
select(UIElement)StyleAnimationAdds one element target.
select(String)StyleAnimationSelects targets by selector on current ModularUI.
style(Property<T>, T)StyleAnimationAnimates one property to a final value.
style(Property<T>, FloatObjectPair...)StyleAnimationAnimates one property with keyframes.
lss(String, Object)StyleAnimationSets target property by LSS property name and value string/object.
duration(float)StyleAnimationSets animation duration in seconds.
delay(float)StyleAnimationSets start delay in seconds.
ease(IEase)StyleAnimationSets easing function.
onInterpolate(BiConsumer<AnimationRuntime, UIElement>)StyleAnimationCalled every animation frame update.
onFinished(Consumer<UIElement>)StyleAnimationCalled when animation finishes per target/property completion flow.
start()ISubscriptionStarts the animation.

Released under the MIT License.