Physical units (compose + code.units)

April 11, 2026 · View on GitHub

What it is

Conversion of real-world measures (millimeters, centimeters, inches) using DisplayMetrics / Resources. It does not follow the “300 dp axis” scaling model; use when you need approximate physical size on the device.

Calculation used

  • toMm(mm, resources, context?): TypedValue.COMPLEX_UNIT_MM divided by densitydp as Float; may use DimenCache (CalcType.UNITIES).
  • toCm(cm, resources, context?): same pattern as toMm via mm (cm×10\text{cm} \times 10) → dp Float (cached like toMm).
  • toInch(inches, resources, context?): TypedValue.COMPLEX_UNIT_IN divided by densitydp Float (cached like toMm).
  • Composable Float / Int .mm, .cm, .inch: wrap the above with LocalDensity / LocalResources / LocalContext. They return a Float in dp-equivalent units (not layout pixels). For Compose layout, use .dp on the result, e.g. 10f.mm.dp.

Pure helpers (convertMmToCm, inchToMm, …) are math-only between unit labels.

Code module (com.appdimens.dynamic.code.units.DimenPhysicalUnits):

  • toPxFromMm / Cm / Inch: direct applyDimensionpixels (no double conversion).
  • toDpFromMm / Cm / Inch: physical unit → dp.
  • toSpFromMm / Cm / Inch: via dp and scaledDensity.
  • radiusFromDiameter / radiusFromCircumference: radius in dp after normalizing the diameter/circumference: MM / CM / INCH via toDp*, DP raw, SP as value × (scaledDensity / density), PX as value / density.

How to use

Compose (import com.appdimens.dynamic.compose.mm, .cm, .inch):

import androidx.compose.ui.unit.dp
import com.appdimens.dynamic.compose.mm
import com.appdimens.dynamic.compose.cm

Modifier.width(10f.mm.dp)
Modifier.height(2.5f.cm.dp)
Composable propertyReceiversReturnTypical use
mmFloat, IntFloat (dp)10f.mm.dpDp for Modifier
cmFloat, IntFloat (dp)2.5f.cm.dp
inchFloat, IntFloat (dp)1f.inch.dp

Composable instance helpers (on DimenPhysicalUnits in the same file)

APIExample
Float.radius(type: UnitType)48f.radius(UnitType.MM) — radius in dp
Number.radius(type: UnitType)48.radius(UnitType.DP)
Float.measureDiameter(isCircumference: Boolean)Toggle diameter vs circumference display
Number.measureDiameter(isCircumference: Boolean)Same

Object DimenPhysicalUnits (Compose) — non-composable helpers

APIRoleExample
toMm(mm, resources, context?)mm → dp FloatDimenPhysicalUnits.toMm(10f, resources)
toCm(cm, resources, context?)cm → dp FloatDimenPhysicalUnits.toCm(2.5f, resources)
toInch(inches, resources, context?)inch → dp FloatDimenPhysicalUnits.toInch(1f, resources)
convertMmToCm / convertMmToInchpure FloatconvertMmToCm(100f)
convertCmToMm / convertCmToInchpure FloatconvertCmToInch(2.54f)
convertInchToCm / convertInchToMmpure FloatconvertInchToMm(1f)
Float.mmToCm(), Number.mmToCm(), …sugar over convert*5f.mmToInch()
radius(diameter, type: UnitType, resources)half-size in dpradius(24f, UnitType.MM, resources)
displayMeasureDiameter(diameter, isCircumference)scale for circumferenceinternal + extensions
unitSizeInDp(type, resources)size of 1.0 logical unit in dp (mm/cm/inch/dp/sp/px normalized to dp)unitSizeInDp(UnitType.MM, resources)

Use UnitType (MM, CM, INCH, SP, DP, PX, …) with radius and unitSizeInDp.

Note: the former name unitSizePerPx was renamed to unitSizeInDp because the values are expressed in dp, not raw pixels. If DOCUMENTATION/KDOC/ estiver desatualizado, gere HTML com Dokka e execute python3 scripts/sync_kdoc_from_dokka_html.py (ver README.md nesta pasta).

Why use it

Specs from print, regulation (touch target in mm), ruler-based prototyping, or physical mockups.

When to use it

  • Minimum tap target in mm.
  • Matching a cm spec.
  • When stakeholders reason in inches / mm, not dp.

Advantages and trade-offs

  • Pros: speaks “real world”; orthogonal to scaling strategies.
  • Cons: real vs nominal dpi varies; not a substitute for scaled / breakpoints for layout grids.

Use for legal / a11y minimums and isolated measurements; build the main grid with scaled (or another strategy).

Back to index