androidx.compose.ui.unit

Interfaces

Density

A density of the screen.

Cmn
FontScaling

Converts TextUnit to Dp and vice-versa.

Cmn
android

Classes

Constraints

Immutable constraints for measuring layouts, used by layouts or layout modifiers to measure their layout children.

Cmn
Dp

Dimension value representing device-independent pixels (dp).

Cmn
DpOffset

A two-dimensional offset using Dp for units.

Cmn
DpRect

A four dimensional bounds using Dp for units

Cmn
DpSize

A two-dimensional Size using Dp for units

Cmn
IntOffset

A two-dimensional position using Int pixels for units.

Cmn
IntRect

An immutable, 2D, axis-aligned, integer bounds rectangle whose coordinates are relative to a given origin.

Cmn
IntSize

A two-dimensional size class used for measuring in Int pixels.

Cmn
TextUnit

The unit used for text related dimension value.

Cmn
TextUnitType

An enum class defining for type of TextUnit.

Cmn
Velocity

A two dimensional velocity in pixels per second.

Cmn

Objects

Annotations

Enums

LayoutDirection

A class for defining layout directions.

Cmn

Top-level functions summary

Constraints
Constraints(minWidth: Int, maxWidth: Int, minHeight: Int, maxHeight: Int)

Create a Constraints.

Cmn
Density
Density(context: Context)

Creates a Density for this Context.

android
Density
Density(density: Float, fontScale: Float)

A density of the screen.

Cmn
inline DpOffset
DpOffset(x: Dp, y: Dp)

Constructs a DpOffset from x and y position Dp values.

Cmn
DpSize
DpSize(width: Dp, height: Dp)

Constructs a DpSize from width and height values.

Cmn
inline IntOffset
IntOffset(x: Int, y: Int)

Constructs a IntOffset from x and y position Int values.

Cmn
IntRect
IntRect(center: IntOffset, radius: Int)

Construct a rectangle that bounds the given circle

Cmn
IntRect
IntRect(offset: IntOffset, size: IntSize)

Construct a rectangle from its left and top edges as well as its width and height.

Cmn
IntRect
IntRect(topLeft: IntOffset, bottomRight: IntOffset)

Construct the smallest rectangle that encloses the given offsets, treating them as vectors from the origin.

Cmn
inline IntSize
IntSize(width: Int, height: Int)

Constructs an IntSize from width and height Int values.

Cmn
TextUnit
TextUnit(value: Float, type: TextUnitType)

Construct a new TextUnit.

Cmn
Velocity
Velocity(x: Float, y: Float)

Constructs an Velocity from the given relative x and y velocities.

Cmn
Dp
lerp(start: Dp, stop: Dp, fraction: Float)

Linearly interpolate between two Dps.

Cmn
DpOffset
lerp(start: DpOffset, stop: DpOffset, fraction: Float)

Linearly interpolate between two DpOffsets.

Cmn
DpSize
lerp(start: DpSize, stop: DpSize, fraction: Float)

Linearly interpolate between two DpSizes.

Cmn
IntOffset
lerp(start: IntOffset, stop: IntOffset, fraction: Float)

Linearly interpolate between two IntOffsets.

Cmn
IntRect
lerp(start: IntRect, stop: IntRect, fraction: Float)

Linearly interpolate between two rectangles.

Cmn
TextUnit
lerp(start: TextUnit, stop: TextUnit, fraction: Float)

Linearly interpolate between two TextUnits.

Cmn
inline Dp
max(a: Dp, b: Dp)
Cmn
inline Dp
min(a: Dp, b: Dp)
Cmn

Extension functions summary

inline Dp
Dp.coerceAtLeast(minimumValue: Dp)

Ensures that this value is not less than the specified minimumValue.

Cmn
inline Dp
Dp.coerceAtMost(maximumValue: Dp)

Ensures that this value is not greater than the specified maximumValue.

Cmn
inline Dp
Dp.coerceIn(minimumValue: Dp, maximumValue: Dp)

Ensures that this value lies in the specified range minimumValue..

Cmn
Constraints
Constraints.constrain(otherConstraints: Constraints)

Takes otherConstraints and returns the result of coercing them in the current constraints.

Cmn
IntSize

Takes a size and returns the closest size to it that satisfies the constraints.

Cmn
Int

Takes a height and returns the closest size to it that satisfies the constraints.

Cmn
Int

Takes a width and returns the closest size to it that satisfies the constraints.

Cmn
Boolean

Takes a size and returns whether it satisfies the current constraints.

Cmn
operator Offset
Cmn
operator Offset
Cmn
Constraints
Constraints.offset(horizontal: Int, vertical: Int)

Returns the Constraints obtained by offsetting the current instance with the given values.

Cmn
operator Offset
Cmn
operator Offset
Cmn
IntOffset

Round a Offset down to the nearest Int coordinates.

Cmn
IntRect

Rounds a Rect to an IntRect

Cmn
IntSize

Convert a Size to an IntSize.

Cmn
inline Dp
Dp.takeOrElse(block: () -> Dp)

If this Dp then this is returned, otherwise block is executed and its result is returned.

Cmn
inline DpOffset

If this DpOffset isSpecified then this is returned, otherwise block is executed and its result is returned.

Cmn
inline DpSize
DpSize.takeOrElse(block: () -> DpSize)

If this DpSize isSpecified then this is returned, otherwise block is executed and its result is returned.

Cmn
inline TextUnit

If this TextUnit then this is returned, otherwise block is executed and its result is returned.

Cmn
inline operator Dp
Float.times(other: Dp)
Cmn
inline operator Dp
Double.times(other: Dp)
Cmn
inline operator Dp
Int.times(other: Dp)
Cmn
inline operator TextUnit

Multiply a TextUnit by a scalar.

Cmn
inline operator TextUnit

Multiply a TextUnit by a scalar.

Cmn
inline operator TextUnit
Int.times(other: TextUnit)

Multiply a TextUnit by a scalar.

Cmn
inline operator DpSize
Int.times(size: DpSize)
Cmn
inline operator DpSize
Cmn
inline operator IntSize
Int.times(size: IntSize)

Returns an IntSize with size's IntSize.width and IntSize.height multiplied by this.

Cmn
IntRect

Convert a IntSize to a IntRect.

Cmn
IntSize

Convert a Size to an IntSize.

Cmn
inline Offset

Converts the IntOffset to an Offset.

Cmn
Rect

Converts an IntRect to a Rect

Cmn
Size
Cmn

Extension properties summary

DpOffset

Returns the DpOffset of the center of the rect from the point of 0, 0 with this DpSize.

Cmn
IntOffset

Returns the IntOffset of the center of the rect from the point of 0, 0 with this IntSize.

Cmn
Dp

Create a Dp using a Double: val left = 10.0 val x = left.dp // -- or -- val y = 10.0.dp

Cmn
Dp

Create a Dp using a Float: val left = 10f val x = left.dp // -- or -- val y = 10f.dp

Cmn
Dp

Create a Dp using an Int: val left = 10 val x = left.dp // -- or -- val y = 10.dp

Cmn
TextUnit

Creates an EM unit TextUnit

Cmn
TextUnit

Creates an EM unit TextUnit

Cmn
TextUnit

Creates an EM unit TextUnit

Cmn
Dp

A height of this Bounds in Dp.

Cmn
Boolean

Return true when it is finite or false when it is Dp.Infinity

Cmn
Boolean

false when this is Dp.Unspecified.

Cmn
Boolean

false when this is DpOffset.Unspecified.

Cmn
Boolean

false when this is DpSize.Unspecified.

Cmn
Boolean

false when this is TextUnit.Unspecified.

Cmn
Boolean

true when this is Dp.Unspecified.

Cmn
Boolean

true when this is DpOffset.Unspecified.

Cmn
Boolean

true when this is DpSize.Unspecified.

Cmn
Boolean

true when this is TextUnit.Unspecified.

Cmn
DpSize

Returns the size of the DpRect.

Cmn
TextUnit

Creates a SP unit TextUnit

Cmn
TextUnit

Creates a SP unit TextUnit

Cmn
TextUnit

Creates a SP unit TextUnit

Cmn
Dp

A width of this Bounds in Dp.

Cmn

Top-level functions

Constraints

fun Constraints(
    minWidth: Int = 0,
    maxWidth: Int = Infinity,
    minHeight: Int = 0,
    maxHeight: Int = Infinity
): Constraints

Create a Constraints. minWidth and minHeight must be positive and maxWidth and maxHeight must be greater than or equal to minWidth and minHeight, respectively, or Infinity.

Density

fun Density(context: Context): Density

Creates a Density for this Context.

Parameters
context: Context

density values will be extracted from this Context

Density

fun Density(density: Float, fontScale: Float = 1.0f): Density

A density of the screen. Used for convert Dp to pixels.

Parameters
density: Float

The logical density of the display. This is a scaling factor for the Dp unit.

fontScale: Float = 1.0f

Current user preference for the scaling factor for fonts.

DpOffset

inline fun DpOffset(x: Dp, y: Dp): DpOffset

Constructs a DpOffset from x and y position Dp values.

fun DpSize(width: Dp, height: Dp): DpSize

Constructs a DpSize from width and height values.

IntOffset

inline fun IntOffset(x: Int, y: Int): IntOffset

Constructs a IntOffset from x and y position Int values.

fun IntRect(center: IntOffset, radius: Int): IntRect

Construct a rectangle that bounds the given circle

Parameters
center: IntOffset

Offset that represents the center of the circle

radius: Int

Radius of the circle to enclose

fun IntRect(offset: IntOffset, size: IntSize): IntRect

Construct a rectangle from its left and top edges as well as its width and height.

Parameters
offset: IntOffset

Offset to represent the top and left parameters of the Rect

size: IntSize

Size to determine the width and height of this IntRect.

fun IntRect(topLeft: IntOffset, bottomRight: IntOffset): IntRect

Construct the smallest rectangle that encloses the given offsets, treating them as vectors from the origin.

Parameters
topLeft: IntOffset

Offset representing the left and top edges of the rectangle

bottomRight: IntOffset

Offset representing the bottom and right edges of the rectangle

IntSize

inline fun IntSize(width: Int, height: Int): IntSize

Constructs an IntSize from width and height Int values.

TextUnit

fun TextUnit(value: Float, type: TextUnitType): TextUnit

Construct a new TextUnit.

Parameters
value: Float

of the dimension

type: TextUnitType

dimension

Velocity

fun Velocity(x: Float, y: Float): Velocity

Constructs an Velocity from the given relative x and y velocities.

Parameters
x: Float

Horizontal component of the velocity in pixels per second

y: Float

Vertical component of the velocity in pixels per second

fun lerp(start: Dp, stop: Dp, fraction: Float): Dp

Linearly interpolate between two Dps.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start (or something equivalent to start), 1.0 meaning that the interpolation has finished, returning stop (or something equivalent to stop), and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid.

fun lerp(start: DpOffset, stop: DpOffset, fraction: Float): DpOffset

Linearly interpolate between two DpOffsets.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start (or something equivalent to start), 1.0 meaning that the interpolation has finished, returning stop (or something equivalent to stop), and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid.

fun lerp(start: DpSize, stop: DpSize, fraction: Float): DpSize

Linearly interpolate between two DpSizes.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start, 1.0 meaning that the interpolation has finished, returning stop, and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid.

fun lerp(start: IntOffset, stop: IntOffset, fraction: Float): IntOffset

Linearly interpolate between two IntOffsets.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start (or something equivalent to start), 1.0 meaning that the interpolation has finished, returning stop (or something equivalent to stop), and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid.

lerp

fun lerp(start: IntRect, stop: IntRect, fraction: Float): IntRect

Linearly interpolate between two rectangles.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start (or something equivalent to start), 1.0 meaning that the interpolation has finished, returning stop (or something equivalent to stop), and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid (and can easily be generated by curves).

Values for fraction are usually obtained from an Animation, such as an AnimationController.

fun lerp(start: TextUnit, stop: TextUnit, fraction: Float): TextUnit

Linearly interpolate between two TextUnits.

The fraction argument represents position on the timeline, with 0.0 meaning that the interpolation has not started, returning start (or something equivalent to start), 1.0 meaning that the interpolation has finished, returning stop (or something equivalent to stop), and values in between meaning that the interpolation is at the relevant point on the timeline between start and stop. The interpolation can be extrapolated beyond 0.0 and 1.0, so negative values and values greater than 1.0 are valid.

Throws
kotlin.IllegalArgumentException

if start and stop have different TextUnitTypes, or either of the two has its TextUnitType equal to TextUnitType.Unspecified.

inline fun max(a: Dp, b: Dp): Dp
inline fun min(a: Dp, b: Dp): Dp

Extension functions

coerceAtLeast

inline fun Dp.coerceAtLeast(minimumValue: Dp): Dp

Ensures that this value is not less than the specified minimumValue.

Returns
Dp

this value if it's greater than or equal to the minimumValue or the minimumValue otherwise.

coerceAtMost

inline fun Dp.coerceAtMost(maximumValue: Dp): Dp

Ensures that this value is not greater than the specified maximumValue.

Returns
Dp

this value if it's less than or equal to the maximumValue or the maximumValue otherwise.

coerceIn

inline fun Dp.coerceIn(minimumValue: Dp, maximumValue: Dp): Dp

Ensures that this value lies in the specified range minimumValue..maximumValue.

Returns
Dp

this value if it's in the range, or minimumValue if this value is less than minimumValue, or maximumValue if this value is greater than maximumValue.

constrain

fun Constraints.constrain(otherConstraints: Constraints): Constraints

Takes otherConstraints and returns the result of coercing them in the current constraints. Note this means that any size satisfying the resulting constraints will satisfy the current constraints, but they might not satisfy the otherConstraints when the two set of constraints are disjoint. Examples (showing only width, height works the same): (minWidth=2, maxWidth=10).constrain(minWidth=7, maxWidth=12) -> (minWidth = 7, maxWidth = 10) (minWidth=2, maxWidth=10).constrain(minWidth=11, maxWidth=12) -> (minWidth=10, maxWidth=10) (minWidth=2, maxWidth=10).constrain(minWidth=5, maxWidth=7) -> (minWidth=5, maxWidth=7)

constrain

fun Constraints.constrain(size: IntSize): IntSize

Takes a size and returns the closest size to it that satisfies the constraints.

constrainHeight

fun Constraints.constrainHeight(height: Int): Int

Takes a height and returns the closest size to it that satisfies the constraints.

constrainWidth

fun Constraints.constrainWidth(width: Int): Int

Takes a width and returns the closest size to it that satisfies the constraints.

isSatisfiedBy

fun Constraints.isSatisfiedBy(size: IntSize): Boolean

Takes a size and returns whether it satisfies the current constraints.

operator fun Offset.minus(offset: IntOffset): Offset
operator fun IntOffset.minus(offset: Offset): Offset
fun Constraints.offset(horizontal: Int = 0, vertical: Int = 0): Constraints

Returns the Constraints obtained by offsetting the current instance with the given values.

operator fun Offset.plus(offset: IntOffset): Offset
operator fun IntOffset.plus(offset: Offset): Offset
fun Offset.round(): IntOffset

Round a Offset down to the nearest Int coordinates.

roundToIntRect

fun Rect.roundToIntRect(): IntRect

Rounds a Rect to an IntRect

roundToIntSize

fun Size.roundToIntSize(): IntSize

Convert a Size to an IntSize. This rounds Size.width and Size.height to the nearest integer.

takeOrElse

inline fun Dp.takeOrElse(block: () -> Dp): Dp

If this Dp then this is returned, otherwise block is executed and its result is returned.

takeOrElse

inline fun DpOffset.takeOrElse(block: () -> DpOffset): DpOffset

If this DpOffset isSpecified then this is returned, otherwise block is executed and its result is returned.

takeOrElse

inline fun DpSize.takeOrElse(block: () -> DpSize): DpSize

If this DpSize isSpecified then this is returned, otherwise block is executed and its result is returned.

takeOrElse

inline fun TextUnit.takeOrElse(block: () -> TextUnit): TextUnit

If this TextUnit then this is returned, otherwise block is executed and its result is returned.

inline operator fun Float.times(other: Dp): Dp
inline operator fun Double.times(other: Dp): Dp
inline operator fun Int.times(other: Dp): Dp
inline operator fun Float.times(other: TextUnit): TextUnit

Multiply a TextUnit by a scalar.

This operation works only if the right operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

inline operator fun Double.times(other: TextUnit): TextUnit

Multiply a TextUnit by a scalar.

This operation works only if the right operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

inline operator fun Int.times(other: TextUnit): TextUnit

Multiply a TextUnit by a scalar.

This operation works only if the right operand is not equal to TextUnit.Unspecified. The result of this operation is the same unit type of the given one.

inline operator fun Int.times(size: DpSize): DpSize
inline operator fun Float.times(size: DpSize): DpSize
inline operator fun Int.times(size: IntSize): IntSize

Returns an IntSize with size's IntSize.width and IntSize.height multiplied by this.

toIntRect

fun IntSize.toIntRect(): IntRect

Convert a IntSize to a IntRect.

toIntSize

fun Size.toIntSize(): IntSize

Convert a Size to an IntSize. This rounds the width and height values down to the nearest integer.

toOffset

inline fun IntOffset.toOffset(): Offset

Converts the IntOffset to an Offset.

toRect

fun IntRect.toRect(): Rect

Converts an IntRect to a Rect

Extension properties

val DpSize.centerDpOffset

Returns the DpOffset of the center of the rect from the point of 0, 0 with this DpSize.

val IntSize.centerIntOffset

Returns the IntOffset of the center of the rect from the point of 0, 0 with this IntSize.

dp

val Double.dpDp

Create a Dp using a Double: val left = 10.0 val x = left.dp // -- or -- val y = 10.0.dp

dp

val Float.dpDp

Create a Dp using a Float: val left = 10f val x = left.dp // -- or -- val y = 10f.dp

dp

val Int.dpDp

Create a Dp using an Int: val left = 10 val x = left.dp // -- or -- val y = 10.dp

em

val Double.emTextUnit

Creates an EM unit TextUnit

em

val Float.emTextUnit

Creates an EM unit TextUnit

em

val Int.emTextUnit

Creates an EM unit TextUnit

height

val DpRect.heightDp

A height of this Bounds in Dp.

isFinite

val Dp.isFiniteBoolean

Return true when it is finite or false when it is Dp.Infinity

isSpecified

val Dp.isSpecifiedBoolean

false when this is Dp.Unspecified.

isSpecified

val DpOffset.isSpecifiedBoolean

false when this is DpOffset.Unspecified.

isSpecified

val DpSize.isSpecifiedBoolean

false when this is DpSize.Unspecified.

isSpecified

val TextUnit.isSpecifiedBoolean

false when this is TextUnit.Unspecified.

isUnspecified

val Dp.isUnspecifiedBoolean

true when this is Dp.Unspecified.

isUnspecified

val DpOffset.isUnspecifiedBoolean

true when this is DpOffset.Unspecified.

isUnspecified

val DpSize.isUnspecifiedBoolean

true when this is DpSize.Unspecified.

isUnspecified

val TextUnit.isUnspecifiedBoolean

true when this is TextUnit.Unspecified.

size

val DpRect.sizeDpSize

Returns the size of the DpRect.

sp

val Double.spTextUnit

Creates a SP unit TextUnit

sp

val Float.spTextUnit

Creates a SP unit TextUnit

sp

val Int.spTextUnit

Creates a SP unit TextUnit

width

val DpRect.widthDp

A width of this Bounds in Dp.