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

IntrinsicMeasureScope

public interface IntrinsicMeasureScope extends Density

Known direct subclasses
MeasureScope

The receiver scope of a layout's measure lambda.

Known indirect subclasses
IntermediateMeasureScope

IntermediateMeasureScope provides access to lookahead results to allow intermediateLayout to leverage lookahead results to define intermediate measurements and placements to gradually converge with lookahead.

LazyLayoutMeasureScope

The receiver scope of a LazyLayout's measure lambda.

SubcomposeIntermediateMeasureScope

SubcomposeIntermediateMeasureScope is the receiver scope for the intermediate measurer policy that gets invoked during the intermediate measure pass.

SubcomposeMeasureScope

The receiver scope of a SubcomposeLayout's measure lambda which adds ability to dynamically subcompose a content during the measuring on top of the features provided by MeasureScope.


The receiver scope of a layout's intrinsic measurements lambdas.

Summary

Public methods

abstract @NonNull LayoutDirection

The LayoutDirection of the Layout or LayoutModifier using the measure scope to measure their children.

default boolean

This indicates whether the ongoing measurement is for lookahead pass.

Inherited methods

From androidx.compose.ui.unit.Density
abstract float

The logical density of the display.

abstract float

Current user preference for the scaling factor for fonts.

default int

Convert Dp to Int by rounding

default int

Convert Sp to Int by rounding

default @NonNull Dp

Convert Sp to Dp.

default @NonNull Dp
orgKt.toDp(int receiver)

Convert an Int pixel value to Dp.

default @NonNull Dp
orgKt.toDp(float receiver)

Convert a Float pixel value to a Dp

default @NonNull DpSize

Convert a Size to a DpSize.

default float
orgKt.toPx(@NonNull Dp receiver)

Convert Dp to pixels.

default float

Convert Sp to pixels.

default @NonNull Rect

Convert a DpRect to a Rect.

default @NonNull Size

Convert a DpSize to a Size.

default @NonNull TextUnit
orgKt.toSp(@NonNull Dp receiver)

Convert Dp to Sp.

default @NonNull TextUnit
orgKt.toSp(int receiver)

Convert an Int pixel value to Sp.

default @NonNull TextUnit
orgKt.toSp(float receiver)

Convert a Float pixel value to a Sp

Public methods

getLayoutDirection

abstract @NonNull LayoutDirection getLayoutDirection()

The LayoutDirection of the Layout or LayoutModifier using the measure scope to measure their children.

isLookingAhead

@ExperimentalComposeUiApi
default boolean isLookingAhead()

This indicates whether the ongoing measurement is for lookahead pass. IntrinsicMeasureScope implementations, especially MeasureScope implementations should override this flag to reflect whether the measurement is intended for lookahead pass.

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector2D
import androidx.compose.animation.core.VectorConverter
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.layout.LookaheadScope
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.IntSize

var sizeAnim by remember {
    mutableStateOf<Animatable<IntSize, AnimationVector2D>?>(null)
}
var lookaheadSize by remember {
    mutableStateOf<IntSize?>(null)
}
val coroutineScope = rememberCoroutineScope()
LookaheadScope {
    // The Box is in a LookaheadScope. This means there will be a lookahead measure pass
    // before the main measure pass.
    // Here we are creating something similar to the `animateContentSize` modifier.
    Box(
        Modifier
            .clipToBounds()
            .layout { measurable, constraints ->
                val placeable = measurable.measure(constraints)

                val measuredSize = IntSize(placeable.width, placeable.height)
                val (width, height) = if (isLookingAhead) {
                    // Record lookahead size if we are in lookahead pass. This lookahead size
                    // will be used for size animation, such that the main measure pass will
                    // gradually change size until it reaches the lookahead size.
                    lookaheadSize = measuredSize
                    measuredSize
                } else {
                    // Since we are in an explicit lookaheadScope, we know the lookahead pass
                    // is guaranteed to happen, therefore the lookahead size that we recorded is
                    // not null.
                    val target = requireNotNull(lookaheadSize)
                    val anim = sizeAnim?.also {
                        coroutineScope.launch { it.animateTo(target) }
                    } ?: Animatable(target, IntSize.VectorConverter)
                    sizeAnim = anim
                    // By returning the animated size only during main pass, we are allowing
                    // lookahead pass to see the future layout past the animation.
                    anim.value
                }

                layout(width, height) {
                    placeable.place(0, 0)
                }
            }) {
        // Some content that changes size
    }
}