androidx.compose.ui.unit

Interfaces

Density

A density of the screen.

Classes

Bounds

A four dimensional bounds using Dp for units

Constraints

Immutable constraints used for measuring child Layouts or LayoutModifiers.

Dp

Dimension value representing device-independent pixels (dp).

DpCubed

Holds a unit of cubed dimensions, such as 1.value * 2.value * 3.dp.

DpInverse

Holds a unit of an inverse dimensions, such as 1.dp / (2.value * 3.dp).

DpSquared

Holds a unit of squared dimensions, such as 1.value * 2.dp.

Duration

A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds.

IntBounds

A four dimensional bounds holder defined by integer pixels.

IntOffset

A two-dimensional position using Int pixels for units

IntSize

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

Position

A two-dimensional position using Dp for units

PxBounds

A four dimensional bounds using pixels for units

PxCubed

Holds a unit of cubed dimensions, such as 1.value * 2.value * 3.px.

PxInverse

Holds a unit of an inverse dimensions, such as 1.px / (2.value * 3.px).

PxSquared

Holds a unit of squared dimensions, such as 1.value * 2.px.

TextUnit

The unit used for text related dimension value.

Uptime

A single point in time with a time base of the system's uptime nanoseconds.

Velocity

A velocity in two dimensions.

Enums

LayoutDirection

A class for defining layout directions.

TextUnitType

An enum class defining for type of thextUnit.

Top-level constants summary

const Long

const Long

const Long

const Long

const Long

const Long

const Long

const Long

const Long

const Long

const Long

Top-level functions summary

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

Create a Constraints.

Density
Density(context: Context)

Creates a Density for this Context.

Density
Density(density: Float, fontScale: Float = 1f)

A density of the screen.

Duration
Duration(days: Long = 0, hours: Long = 0, minutes: Long = 0, seconds: Long = 0, milliseconds: Long = 0, microseconds: Long = 0, nanoseconds: Long = 0)

Constructs a Duration given a series of time intervals in different units.

IntBounds
IntBounds(topLeft: IntOffset, size: IntSize)

IntOffset
IntOffset(x: Int, y: Int)

Constructs a IntOffset from x and y position Int values.

IntSize
IntSize(width: Int, height: Int)

Constructs an IntSize from width and height Int values.

Position
Position(x: Dp, y: Dp)

Constructs a Position from x and y position Dp values.

PxBounds
PxBounds(topLeft: Offset, size: Size)

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

Linearly interpolate between two Dps.

Position
lerp(start: Position, stop: Position, fraction: Float)

Linearly interpolate between two Positions.

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

Linearly interpolate between two IntOffsets.

TextUnit
lerp(a: TextUnit, b: TextUnit, t: Float)

Dp
max(a: Dp, b: Dp)

TextUnit

Returns the smaller value from the given values.

Dp
min(a: Dp, b: Dp)

TextUnit

Returns the smaller value from the given values.

Extension functions summary

For kotlin.Double
operator DpInverse
Double.div(other: Dp)

operator Dp
Double.times(other: Dp)

operator TextUnit

Multiply a TextUnit by a scalar.

For kotlin.Float
operator DpInverse
Float.div(other: Dp)

operator Dp
Float.times(other: Dp)

operator TextUnit

Multiply a TextUnit by a scalar.

For kotlin.Int
operator DpInverse
Int.div(other: Dp)

operator Dp
Int.times(other: Dp)

operator IntSize
Int.times(size: IntSize)

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

operator TextUnit
Int.times(other: TextUnit)

Multiply a TextUnit by a scalar.

For IntBounds
IntOffset

Returns the IntOffset of the center of the IntBounds.

Rect

Convert an IntBounds to a Rect.

IntSize

Convert an IntBounds to an IntSize.

For Size
Offset

Returns the Offset of the center of the rect from the point of 0,0 with this Size.

PxBounds

Convert a Size to a PxBounds.

For PxBounds
Offset

Returns the Offset of the center of the PxBounds.

Rect

Convert a PxBounds to a Rect.

Size

Convert a PxBounds to a Size.

For Dp
Dp
Dp.coerceAtLeast(minimumValue: Dp)

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

Dp
Dp.coerceAtMost(maximumValue: Dp)

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

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

Ensures that this value lies in the specified range minimumValue.

Boolean

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

For TextUnit
TextUnit

Ensures that the value of TextUnit is not less than the specified minimumValue.

TextUnit

Ensures that the value of TextUnit is not greater than the specified maximumValue.

TextUnit
TextUnit.coerceIn(minimumValue: TextUnit, maximumValue: TextUnit)

Ensures that the value of TextUnit lies in the specified range minimumValue.

For Constraints
IntSize

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

Int

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

Int

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

Constraints
Constraints.enforce(otherConstraints: Constraints)

Returns the result of coercing the current constraints in a different set of constraints.

Constraints
Constraints.offset(horizontal: Int = 0, vertical: Int = 0)

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

Boolean

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

For Position
Dp

The magnitude of the offset represented by this Position.

For Duration
Long

Returns the number of whole days spanned by this Duration.

Long

Returns the number of whole hours spanned by this Duration.

Long

Returns number of whole microseconds spanned by this Duration.

Long

Returns number of whole milliseconds spanned by this Duration.

Long

Returns the number of whole minutes spanned by this Duration.

Long

Returns the number of whole seconds spanned by this Duration.

operator Uptime
Duration.plus(uptime: Uptime)

Add a Duration to a Uptime and returns the result as a Uptime.

For Offset
operator Offset

operator Offset

IntOffset

Round a Offset down to the nearest Int coordinates.

For IntOffset
operator Offset

operator Offset

Offset

Converts the IntOffset to an Offset.

For Velocity
operator Velocity

Return the difference of two velocities.

operator Velocity

Return the sum of two velocities.

For IntSize
IntBounds

Convert an IntSize to an IntBounds.

Size

Extension properties summary

For kotlin.Double
Dp

Create a Dp using a Double: val left = 10.

TextUnit

Creates an EM unit TextUnit

TextUnit

Creates a SP unit TextUnit

For kotlin.Float
Dp

Create a Dp using a Float: val left = 10f val x = left.

TextUnit

Creates an EM unit TextUnit

TextUnit

Creates a SP unit TextUnit

For kotlin.Int
Duration

Dp

Create a Dp using an Int: val left = 10 val x = left.

TextUnit

Creates an EM unit TextUnit

Duration

Duration

Duration

Duration

Duration

Duration

TextUnit

Creates a SP unit TextUnit

For kotlin.Long
Duration

Duration

Duration

Duration

Duration

Duration

Duration

For IntSize
IntOffset

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

For Constraints
Boolean

Whether there is exactly one height value that satisfies the constraints.

Boolean

Whether there is exactly one width value that satisfies the constraints.

Boolean

Whether the area of a component respecting these constraints will definitely be 0.

For Bounds
Dp

A height of this Bounds in Dp.

Dp

A width of this Bounds in Dp.

For IntBounds
Int

The height of this IntBounds in integer pixels.

Int

The width of this IntBounds in integer pixels.

For PxBounds
Float

A height of this PxBounds in pixels.

Float

A width of this PxBounds in pixels.

Top-level constants

HoursPerDay

const val HoursPerDay: Long
Value: 24L

MicrosecondsPerMillisecond

const val MicrosecondsPerMillisecond: Long
Value: 1000L

MillisecondsPerSecond

const val MillisecondsPerSecond: Long
Value: 1000L

MinutesPerHour

const val MinutesPerHour: Long
Value: 60L

NanosecondsPerDay

const val NanosecondsPerDay: Long
Value: NanosecondsPerHour * HoursPerDay

NanosecondsPerHour

const val NanosecondsPerHour: Long
Value: NanosecondsPerMinute * MinutesPerHour

NanosecondsPerMicrosecond

const val NanosecondsPerMicrosecond: Long
Value: 1000L

NanosecondsPerMillisecond

const val NanosecondsPerMillisecond: Long
Value: NanosecondsPerMicrosecond * MicrosecondsPerMillisecond

NanosecondsPerMinute

const val NanosecondsPerMinute: Long
Value: NanosecondsPerSecond * SecondsPerMinute

NanosecondsPerSecond

const val NanosecondsPerSecond: Long
Value: NanosecondsPerMillisecond * MillisecondsPerSecond

SecondsPerMinute

const val SecondsPerMinute: Long
Value: 60L

Top-level functions

Constraints

@Stable fun Constraints(
    minWidth: Int = 0,
    maxWidth: Int = Constraints.Infinity,
    minHeight: Int = 0,
    maxHeight: Int = Constraints.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

@Stable fun Density(
    density: Float,
    fontScale: Float = 1f
): 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 = 1f Current user preference for the scaling factor for fonts.

Duration

fun Duration(
    days: Long = 0,
    hours: Long = 0,
    minutes: Long = 0,
    seconds: Long = 0,
    milliseconds: Long = 0,
    microseconds: Long = 0,
    nanoseconds: Long = 0
): Duration

Constructs a Duration given a series of time intervals in different units.

IntBounds

@Stable inline fun IntBounds(
    topLeft: IntOffset,
    size: IntSize
): IntBounds

IntOffset

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

Constructs a IntOffset from x and y position Int values.

IntSize

@Stable fun IntSize(
    width: Int,
    height: Int
): IntSize

Constructs an IntSize from width and height Int values.

Position

@Stable inline fun Position(
    x: Dp,
    y: Dp
): Position

Constructs a Position from x and y position Dp values.

PxBounds

@Stable inline fun PxBounds(
    topLeft: Offset,
    size: Size
): PxBounds

Deprecated.

lerp

@Stable 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.

lerp

@Stable fun lerp(
    start: Position,
    stop: Position,
    fraction: Float
): Position

Linearly interpolate between two Positions.

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

@Stable 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

@Stable fun lerp(
    a: TextUnit,
    b: TextUnit,
    t: Float
): TextUnit

max

@Stable inline fun max(
    a: Dp,
    b: Dp
): Dp

max

@Stable inline fun max(
    a: TextUnit,
    b: TextUnit
): TextUnit

Returns the smaller value from the given values.

This operation works only if all the operands are the same unit type and they are not equal to TextUnit.Inherit. The result of this operation is the same unit type of the given one.

min

@Stable inline fun min(
    a: Dp,
    b: Dp
): Dp

min

@Stable inline fun min(
    a: TextUnit,
    b: TextUnit
): TextUnit

Returns the smaller value from the given values.

This operation works only if all the operands are the same unit type and they are not equal to TextUnit.Inherit. The result of this operation is the same unit type of the given one.

Extension functions

center

@Stable inline fun IntBounds.center(): IntOffset

Returns the IntOffset of the center of the IntBounds.

center

@Stable fun Size.center(): Offset

Returns the Offset of the center of the rect from the point of 0,0 with this Size.

center

@Stable inline fun PxBounds.center(): Offset

Deprecated.

Returns the Offset of the center of the PxBounds.

coerceAtLeast

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

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

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

coerceAtLeast

@Stable inline fun TextUnit.coerceAtLeast(minimumValue: TextUnit): TextUnit

Ensures that the value of TextUnit is not less than the specified minimumValue.

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

coerceAtMost

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

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

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

coerceAtMost

@Stable inline fun TextUnit.coerceAtMost(maximumValue: TextUnit): TextUnit

Ensures that the value of TextUnit is not greater than the specified maximumValue.

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

coerceIn

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

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

Return
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.

coerceIn

@Stable inline fun TextUnit.coerceIn(
    minimumValue: TextUnit,
    maximumValue: TextUnit
): TextUnit

Ensures that the value of TextUnit lies in the specified range minimumValue..maximumValue.

This operation works only if all the operands are the same unit type and they are not equal to TextUnit.Inherit. The result of this operation is the same unit type of the given one.

Return
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

@Stable fun Constraints.constrain(size: IntSize): IntSize

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

constrainHeight

@Stable fun Constraints.constrainHeight(height: Int): Int

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

constrainWidth

@Stable fun Constraints.constrainWidth(width: Int): Int

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

div

@Stable inline operator fun Double.div(other: Dp): DpInverse

div

@Stable inline operator fun Float.div(other: Dp): DpInverse

div

@Stable inline operator fun Int.div(other: Dp): DpInverse

enforce

@Stable fun Constraints.enforce(otherConstraints: Constraints): Constraints

Returns the result of coercing the current constraints in a different set of constraints.

getDistance

@Stable fun Position.getDistance(): Dp

The magnitude of the offset represented by this Position.

inDays

inline fun Duration.inDays(): Long

Returns the number of whole days spanned by this Duration.

inHours

inline fun Duration.inHours(): Long

Returns the number of whole hours spanned by this Duration.

The returned value can be greater than 23.

inMicroseconds

inline fun Duration.inMicroseconds(): Long

Returns number of whole microseconds spanned by this Duration.

inMilliseconds

inline fun Duration.inMilliseconds(): Long

Returns number of whole milliseconds spanned by this Duration.

The returned value can be greater than 999.

inMinutes

inline fun Duration.inMinutes(): Long

Returns the number of whole minutes spanned by this Duration.

The returned value can be greater than 59.

inSeconds

inline fun Duration.inSeconds(): Long

Returns the number of whole seconds spanned by this Duration.

The returned value can be greater than 59.

isFinite

@Stable inline fun Dp.isFinite(): Boolean

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

minus

@Stable operator fun Offset.minus(offset: IntOffset): Offset

minus

@Stable operator fun IntOffset.minus(offset: Offset): Offset

minus

operator fun Velocity.minus(other: Velocity): Velocity

Return the difference of two velocities.

offset

@Stable fun Constraints.offset(
    horizontal: Int = 0,
    vertical: Int = 0
): Constraints

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

plus

operator fun Duration.plus(uptime: Uptime): Uptime

Add a Duration to a Uptime and returns the result as a Uptime.

plus

@Stable operator fun Offset.plus(offset: IntOffset): Offset

plus

@Stable operator fun IntOffset.plus(offset: Offset): Offset

plus

operator fun Velocity.plus(other: Velocity): Velocity

Return the sum of two velocities.

round

@Stable inline fun Offset.round(): IntOffset

Round a Offset down to the nearest Int coordinates.

satisfiedBy

@Stable fun Constraints.satisfiedBy(size: IntSize): Boolean

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

times

@Stable inline operator fun Double.times(other: Dp): Dp

times

@Stable 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.Inherit. The result of this operation is the same unit type of the given one.

times

@Stable inline operator fun Float.times(other: Dp): Dp

times

@Stable 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.Inherit. The result of this operation is the same unit type of the given one.

times

@Stable inline operator fun Int.times(other: Dp): Dp

times

@Stable operator fun Int.times(size: IntSize): IntSize

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

times

@Stable 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.Inherit. The result of this operation is the same unit type of the given one.

toBounds

@Stable fun Size.toBounds(): PxBounds

Deprecated.

Convert a Size to a PxBounds. The left and top are 0.px and the right and bottom are the width and height, respectively.

toBounds

@Stable fun IntSize.toBounds(): IntBounds

Convert an IntSize to an IntBounds. The left and top are 0 and the right and bottom are the width and height, respectively.

toOffset

@Stable inline fun IntOffset.toOffset(): Offset

Converts the IntOffset to an Offset.

toRect

@Stable fun IntBounds.toRect(): Rect

Convert an IntBounds to a Rect.

toRect

@Stable fun PxBounds.toRect(): Rect

Deprecated.

Convert a PxBounds to a Rect.

toSize

@Stable fun IntBounds.toSize(): IntSize

Convert an IntBounds to an IntSize.

toSize

@Stable fun PxBounds.toSize(): Size

Deprecated.

Convert a PxBounds to a Size.

toSize

@Stable fun IntSize.toSize(): Size

Extension properties

center

@Stable val IntSize.center: IntOffset

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

days

inline val Int.days: Duration

days

inline val Long.days: Duration

dp

@Stable inline val Double.dp: Dp

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

dp

@Stable inline val Float.dp: Dp

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

dp

@Stable inline val Int.dp: Dp

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

em

@Stable val Double.em: TextUnit

Creates an EM unit TextUnit

em

@Stable val Float.em: TextUnit

Creates an EM unit TextUnit

em

@Stable val Int.em: TextUnit

Creates an EM unit TextUnit

hasFixedHeight

@Stable val Constraints.hasFixedHeight: Boolean

Whether there is exactly one height value that satisfies the constraints.

hasFixedWidth

@Stable val Constraints.hasFixedWidth: Boolean

Whether there is exactly one width value that satisfies the constraints.

height

@Stable inline val Bounds.height: Dp

A height of this Bounds in Dp.

height

@Stable inline val IntBounds.height: Int

The height of this IntBounds in integer pixels.

height

@Stable inline val PxBounds.height: Float

Deprecated.

A height of this PxBounds in pixels.

hours

inline val Int.hours: Duration

hours

inline val Long.hours: Duration

isZero

@Stable val Constraints.isZero: Boolean

Whether the area of a component respecting these constraints will definitely be 0. This is true when at least one of maxWidth and maxHeight are 0.

microseconds

inline val Int.microseconds: Duration

microseconds

inline val Long.microseconds: Duration

milliseconds

inline val Int.milliseconds: Duration

milliseconds

inline val Long.milliseconds: Duration

minutes

inline val Int.minutes: Duration

minutes

inline val Long.minutes: Duration

nanoseconds

inline val Int.nanoseconds: Duration

nanoseconds

inline val Long.nanoseconds: Duration

seconds

inline val Int.seconds: Duration

seconds

inline val Long.seconds: Duration

sp

@Stable val Double.sp: TextUnit

Creates a SP unit TextUnit

sp

@Stable val Float.sp: TextUnit

Creates a SP unit TextUnit

sp

@Stable val Int.sp: TextUnit

Creates a SP unit TextUnit

width

@Stable inline val Bounds.width: Dp

A width of this Bounds in Dp.

width

@Stable inline val IntBounds.width: Int

The width of this IntBounds in integer pixels.

width

@Stable inline val PxBounds.width: Float

Deprecated.

A width of this PxBounds in pixels.