{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

Density

public interface Density

Known direct subclasses
AwaitPointerEventScope

Receiver scope for awaiting pointer events in a call to PointerInputScope.awaitPointerEventScope.

CacheDrawScope

Handle to a drawing environment that enables caching of content based on the resolved size.

DrawScope

Creates a scoped drawing environment with the provided Canvas.

GraphicsLayerScope

A scope which can be used to define the effects to apply for the content, such as scaling (scaleX, scaleY), rotation (rotationX, rotationY, rotationZ), opacity (alpha), shadow (shadowElevation, shape), and clipping (clip, shape).

InjectionScope

The receiver scope of all input injection lambdas offered in ui-test, such as performTouchInput and performMouseInput.

IntrinsicMeasureScope

The receiver scope of a layout's intrinsic measurements lambdas.

PointerInputScope

Receiver scope for Modifier.pointerInput that permits handling pointer input.

PressGestureScope

Receiver scope for detectTapGestures's onPress lambda.

Known indirect subclasses
CanvasDrawScope

Implementation of DrawScope that issues drawing commands into the specified canvas and bounds via CanvasDrawScope.draw

ContentDrawScope

Receiver scope for drawing content into a layout, where the content can be drawn between other canvas operations.

IntermediateMeasureScope

IntermediateMeasureScope provides access to lookahead results to allow intermediateLayout to leverage lookahead results to define intermediate measurements and placements to gradually converge with lookahead.

KeyInjectionScope

The receiver scope of the key input injection lambda from performKeyInput.

LazyLayoutMeasureScope

The receiver scope of a LazyLayout's measure lambda.

MeasureScope

The receiver scope of a layout's measure lambda.

MouseInjectionScope

The receiver scope of the mouse input injection lambda from performMouseInput.

MultiModalInjectionScope

The receiver scope of the multi-modal input injection lambda from performMultiModalInput.

RotaryInjectionScope

The receiver scope of rotary input injection lambda from performRotaryScrollInput.

SubcomposeIntermediateMeasureScope

SubcomposeIntermediateMeasureScope is the receiver scope for the intermediate measurer policy that gets invoked during the intermediate measure pass.

SubcomposeMeasureScope

The receiver scope of a SubcomposeLayout's measure lambda which adds ability to dynamically subcompose a content during the measuring on top of the features provided by MeasureScope.

TouchInjectionScope

The receiver scope of the touch input injection lambda from performTouchInput.


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

import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp

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

Summary

Public methods

abstract float

The logical density of the display.

abstract float

Current user preference for the scaling factor for fonts.

default int
roundToPx(@NonNull Dp receiver)

Convert Dp to Int by rounding

default int

Convert Sp to Int by rounding

default @NonNull Dp
toDp(float receiver)

Convert a Float pixel value to a Dp

default @NonNull Dp
toDp(int receiver)

Convert an Int pixel value to Dp.

default @NonNull Dp
toDp(@NonNull TextUnit receiver)

Convert Sp to Dp.

default @NonNull DpSize
toDpSize(@NonNull Size receiver)

Convert a Size to a DpSize.

default float
toPx(@NonNull Dp receiver)

Convert Dp to pixels.

default float
toPx(@NonNull TextUnit receiver)

Convert Sp to pixels.

default @NonNull Rect
toRect(@NonNull DpRect receiver)

Convert a DpRect to a Rect.

default @NonNull Size
toSize(@NonNull DpSize receiver)

Convert a DpSize to a Size.

default @NonNull TextUnit
toSp(@NonNull Dp receiver)

Convert Dp to Sp.

default @NonNull TextUnit
toSp(float receiver)

Convert a Float pixel value to a Sp

default @NonNull TextUnit
toSp(int receiver)

Convert an Int pixel value to Sp.

Public methods

getDensity

abstract float getDensity()

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

getFontScale

abstract float getFontScale()

Current user preference for the scaling factor for fonts.

roundToPx

default int roundToPx(@NonNull Dp receiver)

Convert Dp to Int by rounding

roundToPx

default int roundToPx(@NonNull TextUnit receiver)

Convert Sp to Int by rounding

toDp

default @NonNull Dp toDp(float receiver)

Convert a Float pixel value to a Dp

toDp

default @NonNull Dp toDp(int receiver)

Convert an Int pixel value to Dp.

toDp

default @NonNull Dp toDp(@NonNull TextUnit receiver)

Convert Sp to Dp.

Throws
kotlin.IllegalStateException

if TextUnit other than SP unit is specified.

toDpSize

default @NonNull DpSize toDpSize(@NonNull Size receiver)

Convert a Size to a DpSize.

toPx

default float toPx(@NonNull Dp receiver)

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

toPx

default float toPx(@NonNull TextUnit receiver)

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

Throws
kotlin.IllegalStateException

if TextUnit other than SP unit is specified.

toRect

default @NonNull Rect toRect(@NonNull DpRect receiver)

Convert a DpRect to a Rect.

toSize

default @NonNull Size toSize(@NonNull DpSize receiver)

Convert a DpSize to a Size.

toSp

default @NonNull TextUnit toSp(@NonNull Dp receiver)

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

toSp

default @NonNull TextUnit toSp(float receiver)

Convert a Float pixel value to a Sp

toSp

default @NonNull TextUnit toSp(int receiver)

Convert an Int pixel value to Sp.