GltfAnimation


public final class GltfAnimation


GltfAnimation represents an animation in a GltfModelEntity.

Multiple animations can play simultaneously. If multiple animations affect the same node, the animation with the highest index in the glTF file takes precedence. Specifically, only the state of the highest-index animation is reflected in the scene for a given frame. Updates from other lower-index animations (such as node transforms) for the same target will be overwritten.

A GltfAnimation cannot be used once its associated GltfModelEntity has been destroyed.

Summary

Nested types

public final class GltfAnimation.AnimationState

Specifies the current animation state of this GltfAnimation.

Public methods

final void

Registers a listener to be invoked on the main thread when the animation state of the GltfAnimation changes.

final void

Registers a listener to be invoked when the animation state of this GltfAnimation changes.

final @NonNull GltfAnimation.AnimationState

The current state of this animation.

final @NonNull Duration
@RequiresApi(value = 26)
getDuration()

The duration of this animation.

final int

The index of this animation in the source glTF model.

final boolean

Whether this animation should loop when playing.

final String

The name of this animation, or null if the animation is unnamed.

final float

The playback speed multiplier for this animation.

final void

Pauses this animation.

final void

Unregisters a previously registered animation state update listener.

final void

Resumes this animation.

final void

This method is deprecated. Seeking animation is no longer supported.

final void

Whether this animation should loop when playing.

final void
@MainThread
setSpeed(float value)

The playback speed multiplier for this animation.

final void

Starts playing this animation.

final void

This method is deprecated. Use GltfAnimation.loop and GltfAnimation.speed properties with parameterless start() instead.

final void

Stops this animation.

Public methods

addAnimationStateListener

Added in 1.0.0
public final void addAnimationStateListener(
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)

Registers a listener to be invoked on the main thread when the animation state of the GltfAnimation changes.

Parameters
@NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener

The listener to invoke when the state changes. It receives the new AnimationState.

addAnimationStateListener

Added in 1.0.0
public final void addAnimationStateListener(
    @NonNull Executor executor,
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)

Registers a listener to be invoked when the animation state of this GltfAnimation changes.

Parameters
@NonNull Executor executor

The executor on which the listener will be invoked.

@NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener

The listener to invoke when the state changes. It receives the new AnimationState.

getAnimationState

Added in 1.0.0
@MainThread
public final @NonNull GltfAnimation.AnimationState getAnimationState()

The current state of this animation.

getDuration

Added in 1.0.0
@RequiresApi(value = 26)
public final @NonNull Duration getDuration()

The duration of this animation.

getIndex

Added in 1.0.0
public final int getIndex()

The index of this animation in the source glTF model.

getLoop

Added in 1.1.0-alpha01
@MainThread
public final boolean getLoop()

Whether this animation should loop when playing.

The default value is false (playback does not loop). When looping is disabled (false) and playback reaches the end of the animation, the animation state transitions to AnimationState.STOPPED, while remaining clamped at the final frame pose.

Changes to the loop configuration only take effect during start. Modifying this property while the animation is actively playing is currently not supported. The setter will remain annotated with ExperimentalGltfAnimationApi until dynamic loop updates during active playback are supported.

getName

Added in 1.0.0
public final String getName()

The name of this animation, or null if the animation is unnamed.

getSpeed

Added in 1.1.0-alpha01
@MainThread
public final float getSpeed()

The playback speed multiplier for this animation.

This can be changed while the animation is playing or paused.

The default playback speed is 1.0f. The speed multiplier determines the playback rate:

  • 1.0: Normal speed.

  • > 1.0: Faster playback.

  • > 0.0 and < 1.0: Slower playback (e.g., 0.5 is half speed).

  • 0.0: Freezes the animation at the current frame while keeping it active (unlike pausing).

  • < 0.0: Plays the animation in reverse.

Note on Reverse Playback: When playing in reverse without looping (loop set to false), starting the animation from the beginning causes it to reach the end of playback immediately. Enable looping via loop (setting loop to true) to play the animation in reverse continuously.

pause

Added in 1.0.0
@MainThread
public final void pause()

Pauses this animation.

This freezes the animation at the current frame and transitions the animation state to AnimationState.PAUSED. Use resume to continue playback.

Note: Calling start while in the AnimationState.PAUSED state will reset the playback time to the start of the animation, and transitions the state to AnimationState.PLAYING.

removeAnimationStateListener

Added in 1.0.0
public final void removeAnimationStateListener(
    @NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener
)

Unregisters a previously registered animation state update listener.

Parameters
@NonNull Consumer<@NonNull GltfAnimation.AnimationState> listener

The listener to remove.

resume

Added in 1.0.0
@MainThread
public final void resume()

Resumes this animation.

This continues the animation from the point where it was paused and transitions the animation state to AnimationState.PLAYING.

Note: Calling resume while in the AnimationState.PLAYING and AnimationState.STOPPED state will have no effect.

seekTo

Added in 1.0.0
Deprecated in 1.1.0-alpha01
@RequiresApi(value = 26)
@MainThread
public final void seekTo(@NonNull Duration time)

Seeks this animation to the specified time offset.

Parameters
@NonNull Duration time

The offset from the beginning of the animation.

Throws
IllegalArgumentException

if time is negative.

setLoop

Added in 1.1.0-alpha01
@MainThread
@ExperimentalGltfAnimationApi
public final void setLoop(boolean value)

Whether this animation should loop when playing.

The default value is false (playback does not loop). When looping is disabled (false) and playback reaches the end of the animation, the animation state transitions to AnimationState.STOPPED, while remaining clamped at the final frame pose.

Changes to the loop configuration only take effect during start. Modifying this property while the animation is actively playing is currently not supported. The setter will remain annotated with ExperimentalGltfAnimationApi until dynamic loop updates during active playback are supported.

setSpeed

Added in 1.0.0
@MainThread
public final void setSpeed(float value)

The playback speed multiplier for this animation.

This can be changed while the animation is playing or paused.

The default playback speed is 1.0f. The speed multiplier determines the playback rate:

  • 1.0: Normal speed.

  • > 1.0: Faster playback.

  • > 0.0 and < 1.0: Slower playback (e.g., 0.5 is half speed).

  • 0.0: Freezes the animation at the current frame while keeping it active (unlike pausing).

  • < 0.0: Plays the animation in reverse.

Note on Reverse Playback: When playing in reverse without looping (loop set to false), starting the animation from the beginning causes it to reach the end of playback immediately. Enable looping via loop (setting loop to true) to play the animation in reverse continuously.

start

Added in 1.0.0
@MainThread
public final void start()

Starts playing this animation.

This transitions the animation state to AnimationState.PLAYING. By default, the animation plays once at normal speed (1.0f) without looping unless configured otherwise via loop and speed. Calling start while the animation is currently playing or paused will restart playback from the beginning.

start

Added in 1.0.0
Deprecated in 1.1.0-alpha01
@RequiresApi(value = 26)
@ExperimentalGltfAnimationApi
@MainThread
public final void start(@NonNull GltfAnimationStartOptions options)

Starts playing this animation with the specified options.

Parameters
@NonNull GltfAnimationStartOptions options

Configuration options for starting the animation.

stop

Added in 1.0.0
@MainThread
public final void stop()

Stops this animation.

This resets the playback time to 0 and transitions the animation state to AnimationState.STOPPED. If this animation is not currently playing or pausing, this method has no effect.