Android 12 Developer Preview is here! Try it out, and give us your feedback!

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(LocalDensity.current) { 16.dp.toPx() }

Summary

Public methods
open Int

Convert Dp to Int by rounding

open Int

Convert Sp to Int by rounding

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 Float

Convert Dp to pixels.

open Float

Convert Sp to pixels.

open Rect

Convert a DpRect 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

Properties
abstract Float

The logical density of the display.

abstract Float

Current user preference for the scaling factor for fonts.

Public methods

roundToPx

@Stable open fun Dp.roundToPx(): Int

Convert Dp to Int by rounding

roundToPx

@Stable open fun TextUnit.roundToPx(): Int

Convert Sp to Int by rounding

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