Density

@Immutable interface Density
androidx.compose.ui.unit.Density

A density of the screen. Used for the conversions between Dp, Px, Int and TextUnit.


val sizeInPx = with(DensityAmbient.current) { 16.dp.toPx() }

Summary

Public methods
open Dp

Convert Sp to Dp.

open Dp

Convert an Int pixel value to Dp.

open Dp

Convert a Float pixel value to a Dp

open Int

Convert Dp to Int by rounding

open Int

Convert Sp to Int by rounding

open Float

Convert Dp to pixels.

open Float

Convert Sp to pixels.

open Rect

Convert a Bounds to a Rect.

open TextUnit

Convert Dp to Sp.

open TextUnit

Convert an Int pixel value to Sp.

open TextUnit

Convert a Float pixel value to a Sp

Extension functions
From androidx.compose.foundation.layout
Constraints

Creates the Constraints corresponding to the current DpConstraints.

DpConstraints

Creates the DpConstraints corresponding to the current Constraints.

Properties
abstract Float

The logical density of the display.

abstract Float

Current user preference for the scaling factor for fonts.

Public methods

toDp

@Stable open fun TextUnit.toDp(): Dp

Convert Sp to Dp.

Exceptions
IllegalStateException if TextUnit other than SP unit is specified.

toDp

@Stable open fun Int.toDp(): Dp

Convert an Int pixel value to Dp.

toDp

@Stable open fun Float.toDp(): Dp

Convert a Float pixel value to a Dp

toIntPx

@Stable open fun Dp.toIntPx(): Int

Convert Dp to Int by rounding

toIntPx

@Stable open fun TextUnit.toIntPx(): Int

Convert Sp to Int by rounding

toPx

@Stable open fun Dp.toPx(): Float

Convert Dp to pixels. Pixels are used to paint to Canvas.

toPx

@Stable open fun TextUnit.toPx(): Float

Convert Sp to pixels. Pixels are used to paint to Canvas.

Exceptions
IllegalStateException if TextUnit other than SP unit is specified.

toRect

@Stable open fun Bounds.toRect(): Rect

Convert a Bounds to a Rect.

toSp

@Stable open fun Dp.toSp(): TextUnit

Convert Dp to Sp. Sp is used for font size, etc.

toSp

@Stable open fun Int.toSp(): TextUnit

Convert an Int pixel value to Sp.

toSp

@Stable open fun Float.toSp(): TextUnit

Convert a Float pixel value to a Sp

Properties

density

@Stable abstract val density: Float

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

fontScale

@Stable abstract val fontScale: Float

Current user preference for the scaling factor for fonts.