DotLottieController Class (for Jetpack Compose)

The DotLottieController class provides a comprehensive interface for managing the playback and configuration of dotLottie animations within Jetpack Compose. This reference outlines its properties, methods, and the mechanism for event handling.

The DotLottieController provides additional methods for programmatic control in Compose and supports all the same functionality as the traditional UI DotLottieAnimation view.

Properties

PropertyDescriptionTypeAccess
isPlayingChecks if the animation is playing.BooleanRead
isLoadedDetermines if the animation has been loaded successfully.BooleanRead
isCompleteChecks if the animation has completed playing.BooleanRead
isStoppedIndicates whether the animation is stopped.BooleanRead
isPausedIndicates whether the animation is paused.BooleanRead
speedThe playback speed of the animation.FloatRead
loopDetermines whether the animation should loop.BooleanRead
autoplayDetermines if the animation will start playing automatically.BooleanRead
totalFramesThe total number of frames in the animation.FloatRead
currentFrameThe current frame of the animation.FloatRead
playModeThe play mode of the animation (e.g., forward, reverse).ModeRead
segmentThe start and end frames of the current segment being played.Pair<Float, Float>?Read
durationThe duration of the animation in seconds.FloatRead
loopCountThe number of times the animation will loop.UIntRead
useFrameInterpolationChecks if frame interpolation is enabled for smoother animation.BooleanRead
activeThemeIdRetrieves the ID of the currently active theme.StringRead
activeAnimationIdRetrieves the ID of the currently active animation.StringRead

Methods

Playback Control

MethodDescription
play()Starts or resumes the animation.
pause()Pauses the animation.
stop()Stops the animation and resets to the beginning.
setSpeed(speed: Float)Sets the playback speed of the animation.
setLoop(loop: Boolean)Sets whether the animation should loop.
setPlayMode(mode: Mode)Sets the play mode of the animation (Forward, Reverse, etc.).

Frame Manipulation

MethodDescription
setFrame(frame: Float)Sets the current frame of the animation.
setSegment(firstFrame: Float, lastFrame: Float)Defines the start and end frames for a segment of the animation to play.
freeze()Freezes the animation at the current frame.
unFreeze()Unfreezes the animation, allowing it to continue.
setUseFrameInterpolation(enable: Boolean)Enables or disables frame interpolation.

Animation Loading and Configuration

MethodDescription
loadAnimation(animationId: String)Loads an animation by its ID from multi-animation .lottie files.
manifest(): Manifest?Retrieves the dotLottie manifest information.
resize(width: UInt, height: UInt)Resizes the animation view.
setLayout(fit: Fit, alignment: LayoutUtil.Alignment)Sets the animation layout configuration.

Theming

MethodDescription
setTheme(themeId: String)Sets a theme from the .lottie file by its ID.
setThemeData(themeData: String)Sets a theme from theme JSON data.
resetTheme()Resets the theme to the default state.
setSlots(slotsData: String)Sets slot data for dynamic content replacement.

State Machine Control

MethodDescription
stateMachineLoad(stateMachineId: String): BooleanLoads a state machine by its ID.
stateMachineStart(openUrl: OpenUrlPolicy = createDefaultOpenUrlPolicy(), onOpenUrl: ((url: String) -> Unit)? = null): BooleanStarts the loaded state machine with URL handling configuration.
stateMachineStop(): BooleanStops the state machine.
stateMachineFire(eventName: String)Fires a named event to the state machine.
stateMachinePostEvent(event: Event): BooleanPosts custom events (gestures, etc.) to the state machine.
stateMachineSetNumericInput(name: String, value: Float)Sets a numeric input value.
stateMachineSetStringInput(name: String, value: String)Sets a string input value.
stateMachineSetBooleanInput(name: String, value: Boolean)Sets a boolean input value.
stateMachineGetNumericInput(name: String): FloatGets a numeric input value.
stateMachineGetStringInput(name: String): StringGets a string input value.
stateMachineGetBooleanInput(name: String): BooleanGets a boolean input value.
stateMachineCurrentState(): StringReturns the current state machine state.

Event Management

MethodDescription
addEventListener(listener: DotLottieEventListener)Adds an event listener to receive animation events.
removeEventListener(listener: DotLottieEventListener)Removes an event listener.
clearEventListeners()Removes all event listeners.
stateMachineAddEventListener(listener: StateMachineEventListener)Adds a listener for state machine events.
stateMachineRemoveEventListener(listener: StateMachineEventListener)Removes a state machine event listener.

Usage Example

Here’s a comprehensive example of how to use the DotLottieController in Jetpack Compose:

import com.lottiefiles.dotlottie.core.compose.ui.DotLottieAnimation
import com.lottiefiles.dotlottie.core.compose.runtime.DotLottieController
import com.lottiefiles.dotlottie.core.util.DotLottieSource
import com.dotlottie.dlplayer.Mode
@Composable
fun ControlledAnimationExample() {
val dotLottieController = remember { DotLottieController() }
// This effect runs once when the composable enters the composition
LaunchedEffect(Unit) {
dotLottieController.setLoop(true)
dotLottieController.setSpeed(3f)
// You can control the animation directly using the controller
dotLottieController.play()
// In a real app, you would call these methods from button clicks
// or other user events.
// For example:
// Button(onClick = { dotLottieController.pause() }) { Text("Pause") }
// Button(onClick = { dotLottieController.stop() }) { Text("Stop") }
}
DotLottieAnimation(
source = DotLottieSource.Url("https://lottiefiles-mobile-templates.s3.amazonaws.com/ar-stickers/swag_sticker_piggy.lottie"),
autoplay = false,
loop = false,
speed = 1f,
useFrameInterpolation = false,
playMode = Mode.FORWARD,
controller = dotLottieController
)
}

Event Handling

The DotLottieController class allows for the registration of event listeners that implement the DotLottieEventListener interface. These listeners can respond to various animation events, including:

  • onFrame(frameNo: Float)
  • onPause()
  • onStop()
  • onPlay()
  • onComplete()
  • onLoad()
  • onLoadError()
  • onLoop()
  • onFreeze()
  • onUnFreeze()
  • onRender()
  • onDestroy()

For state machine events, use the StateMachineEventListener interface.

This API reference provides a detailed overview of the DotLottieController class, enabling developers to fully leverage its capabilities for controlling dotLottie animations within their Jetpack Compose applications.