RiveCanvasSession

@RequiresApi(value = 29)
class RiveCanvasSession(riveWorker: RiveWorker, artboard: Artboard, stateMachine: StateMachine, viewModelInstance: ViewModelInstance? = null, fit: Fit = RenderingDefaults.defaultFit(), @ColorInt clearColor: Int = RenderingDefaults.CLEAR_COLOR) : AutoCloseable

An entry point for Rive to render to a hardware accelerated Canvas using hardware bitmaps.

⚠️ This class must be closed when you no longer need it to free its resources. Call this only from the main thread.

The session manages an advance and render loop after calling beginPlaying. While this render loop will produce bitmaps, it is the caller's responsibility to present them on a canvas using draw. Collect frameAvailable to know when new frames are available after rendering.

Callers must pass touch events to the session with onTouchEvent to apply them to the state machine. Coordinates of the events are expected to be in the same space as the destination canvas of draw, and will be mapped into the render region.

All instance methods are expected to be called on Android's main thread. Calling instance methods after close throws IllegalStateException (except repeated close, which remains idempotent).

Call setRegion with the destination rectangle where frames should be presented. The render buffer dimensions are derived from this region's width and height.

If viewModelInstance is supplied, this session binds it eagerly during initialization.

This session is backed by a HardwareRenderBuffer. It owns that buffer's lifecycle and frame loop, but not the supplied Rive resources. These must be created and closed by the caller, and must outlive this session. The session will check that the supplied resources are from the same RiveWorker instance, but does not check that they are valid or properly initialized.

The supplied riveWorker must be polled for messages. This is done separately from this session using RiveWorker.beginPolling, so that the caller can manage the worker lifecycle and share it across multiple sessions.

Parameters

riveWorker

The Rive worker that holds the resources to render. See the note above regarding polling.

artboard

The artboard to render. Must be from the supplied riveWorker.

stateMachine

The state machine to advance and render. Must be from the supplied artboard.

viewModelInstance

An optional view model instance to bind to the state machine. Must be from the supplied riveWorker.

fit

The Fit to use when rendering. This controls how the artboard is scaled to fit the target surface. Defaults to RenderingDefaults.defaultFit.

clearColor

The color used to clear the draw region before drawing each frame. Defaults to RenderingDefaults.CLEAR_COLOR.

Constructors

Link copied to clipboard
constructor(riveWorker: RiveWorker, artboard: Artboard, stateMachine: StateMachine, viewModelInstance: ViewModelInstance? = null, fit: Fit = RenderingDefaults.defaultFit(), @ColorInt clearColor: Int = RenderingDefaults.CLEAR_COLOR)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val frameAvailable: SharedFlow<Unit>

Emits whenever a newly published render frame becomes visible to this session.

Functions

Link copied to clipboard
suspend fun beginPlaying(lifecycle: Lifecycle, ticker: FrameTicker = ChoreographerFrameTicker)

Runs the advance and render loop while lifecycle is RESUMED.

Link copied to clipboard
open override fun close()
Link copied to clipboard
fun draw(canvas: Canvas)

Draws the latest rendered bitmap into the configured draw region of the canvas, if available.

Link copied to clipboard

Forwards touch events to the state machine pointer APIs.

Link copied to clipboard

Update the target draw region for rendering. This should be called at least once to initialize the session, and whenever the region changes.