{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

MonotonicFrameClock

public interface MonotonicFrameClock extends CoroutineContext.Element

Known direct subclasses
AndroidUiFrameClock
BroadcastFrameClock

A simple frame clock.

PausableMonotonicFrameClock

A MonotonicFrameClock wrapper that can be paused and resumed.

TestMonotonicFrameClock

A MonotonicFrameClock with a time source controlled by a kotlinx-coroutines-test TestCoroutineScheduler.


Provides a time source for display frames and the ability to perform an action on the next frame. This may be used for matching timing with the refresh rate of a display or otherwise synchronizing work with a desired frame rate.

Summary

Nested types

public static class MonotonicFrameClock.Key implements CoroutineContext.Key

Public methods

default @NonNull CoroutineContext.Key<@NonNull ?>
abstract @NonNull R
<R extends Object> withFrameNanos(
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

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.

Extension functions

default final @NonNull R
<R extends Object> MonotonicFrameClockKt.withFrameMillis(
    @NonNull MonotonicFrameClock receiver,
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

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

Inherited methods

From kotlin.coroutines.CoroutineContext
From kotlin.coroutines.CoroutineContext.Element
default @NonNull R
<R extends Object> fold(
    @NonNull R initial,
    @NonNull Function2<@NonNull R, @NonNull CoroutineContext.Element, @NonNull R> operation
)
default E
default @NonNull CoroutineContext

Public methods

getKey

default @NonNull CoroutineContext.Key<@NonNull ?> getKey()

withFrameNanos

abstract @NonNull R <R extends Object> withFrameNanos(
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

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 strictly monotonically increasing; after a call to withFrameNanos completes it must not provide the same value again for a subsequent call.

Extension functions

MonotonicFrameClockKt.withFrameMillis

default final @NonNull R <R extends Object> MonotonicFrameClockKt.withFrameMillis(
    @NonNull MonotonicFrameClock receiver,
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

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

frameTimeMillis 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 MonotonicFrameClock.withFrameMillis is implementation defined. Time values provided are monotonically increasing; after a call to withFrameMillis completes it must not provide a smaller value for a subsequent call.