Android 12 Developer Preview is here! Try it out, and give us your feedback!

PausableMonotonicFrameClock

class PausableMonotonicFrameClock : MonotonicFrameClock
kotlin.Any
   ↳ androidx.compose.runtime.PausableMonotonicFrameClock

A MonotonicFrameClock wrapper that can be paused and resumed.

A paused clock will not dispatch withFrameNanos events until it is resumed. Pausing a clock does not stop or change the frame times reported to withFrameNanos calls; the clock times reported will always remain consistent with frameClock.

PausableMonotonicFrameClock should be used in cases where frames should not be produced under some conditions, such as when a window hosting a UI is not currently visible. As clock times are not altered from the source frameClock, animations in progress may be fully complete by the time the clock is resumed and a new frame is produced.

Summary

Public constructors

A MonotonicFrameClock wrapper that can be paused and resumed.

Public methods
Unit

Pause the generation of frames.

Unit

Resume the generation of frames.

suspend R
withFrameNanos(onFrame: (frameTimeNanos: Long) -> R)

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

Inherited extension functions
From androidx.compose.runtime
suspend R
MonotonicFrameClock.withFrameMillis(crossinline onFrame: (frameTimeMillis: Long) -> R)

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

Properties
Boolean

true if this clock is currently paused or false if this clock is currently resumed.

Inherited properties

Public constructors

<init>

PausableMonotonicFrameClock(frameClock: MonotonicFrameClock)

A MonotonicFrameClock wrapper that can be paused and resumed.

A paused clock will not dispatch withFrameNanos events until it is resumed. Pausing a clock does not stop or change the frame times reported to withFrameNanos calls; the clock times reported will always remain consistent with frameClock.

PausableMonotonicFrameClock should be used in cases where frames should not be produced under some conditions, such as when a window hosting a UI is not currently visible. As clock times are not altered from the source frameClock, animations in progress may be fully complete by the time the clock is resumed and a new frame is produced.

Public methods

pause

fun pause(): Unit

Pause the generation of frames. Pausing a clock that is already paused has no effect. While the clock is paused any calls to withFrameNanos will suspend until the clock is resumed before delegating to the wrapped frameClock's withFrameNanos method. Call resume to resume generating frames.

resume

fun resume(): Unit

Resume the generation of frames. Any queued calls to withFrameNanos will resume and delegate to the wrapped frameClock's withFrameNanos method.

withFrameNanos

suspend fun <R> withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

frameTimeNanos should be used when calculating animation time deltas from frame to frame as it may be normalized to the target time for the frame, not necessarily a direct, "now" value.

The time base of the value provided by withFrameNanos is implementation defined. Time values provided are monotonically increasing; after a call to withFrameNanos completes it must not provide the same value again for a subsequent call.

Properties

isPaused

val isPaused: Boolean

true if this clock is currently paused or false if this clock is currently resumed. A PausableMonotonicFrameClock is not paused at construction time.