Density
A density of the screen. Used for the conversions between pixels, [Dp], [Int] and [TextUnit].
Density
Interface
Common
@JvmDefaultWithCompatibility
interface Density : FontScaling
A density of the screen. Used for the conversions between pixels, Dp, Int and TextUnit.
Properties
Common
val density: Float
The logical density of the display. This is a scaling factor for the Dp unit.
Functions
fun Dp.toPx(): Float
Convert Dp to pixels. Pixels are used to paint to Canvas.
fun Dp.roundToPx(): Int
Convert Dp to Int by rounding
fun TextUnit.toPx(): Float
Convert Sp to pixels. Pixels are used to paint to Canvas.
fun TextUnit.roundToPx(): Int
Convert Sp to Int by rounding
fun Int.toDp(): Dp
Convert an Int pixel value to Dp.
fun Int.toSp(): TextUnit
Convert an Int pixel value to Sp.
fun Float.toDp(): Dp
Convert a Float pixel value to a Dp
fun Float.toSp(): TextUnit
Convert a Float pixel value to a Sp
fun DpRect.toRect(): Rect
Convert a DpRect to a Rect.
fun DpSize.toSize(): Size
Convert a DpSize to a Size.
fun Size.toDpSize(): DpSize
Convert a Size to a DpSize.
Code Examples
WithDensitySample
@Composable
@Suppress("UNUSED_VARIABLE")
fun WithDensitySample() {
val sizeInPx = with(LocalDensity.current) { 16.dp.toPx() }
}
