Dp
-
Cmn
value class Dp : Comparable
Dimension value representing device-independent pixels (dp). Component APIs specify their dimensions such as line thickness in DP with Dp objects. Hairline (1 pixel) thickness may be specified with Hairline, a dimension that take up no space. Dp are normally defined using dp, which can be applied to Int, Double, and Float.
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.padding import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp Box( Modifier.padding( 10.dp, // Int 10f.dp, // Float 20.0.dp, // Double 10.dp, ) )
Drawing and Layout are done in pixels. To retrieve the pixel size of a Dp, use Density.toPx:
import androidx.compose.foundation.Canvas import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.graphics.drawscope.inset import androidx.compose.ui.unit.dp val lineThickness = 6.dp Canvas(Modifier.fillMaxSize()) { val lineThicknessPx = lineThickness.toPx() inset(lineThicknessPx / 2) { drawRect(Color.Red, style = Stroke(lineThicknessPx)) } }
Summary
Nested types |
|---|
object Dp.Companion |
Public companion properties |
||
|---|---|---|
Dp |
A dimension used to represent a hairline drawing element. |
Cmn
|
Dp |
Infinite dp dimension. |
Cmn
|
Dp |
Constant that means unspecified Dp. |
Cmn
|
Public functions |
||
|---|---|---|
open operator Int |
Support comparing Dimensions with comparison operators. |
Cmn
|
inline operator Float |
Divide by another Dp to get a scalar. |
Cmn
|
inline operator Dp |
Divide a Dp by a scalar. |
Cmn
|
inline operator Dp |
Cmn
|
|
inline operator Dp |
Subtract a Dp from another one. |
Cmn
|
inline operator Dp |
Add two |
Cmn
|
inline operator Dp |
Multiply a Dp by a scalar. |
Cmn
|
inline operator Dp |
Cmn
|
|
open String |
toString() |
Cmn
|
inline operator Dp |
This is the same as multiplying the Dp by -1.0. |
Cmn
|
Extension functions |
||
|---|---|---|
Unit |
Dp.assertIsEqualTo(expected: Dp, subject: String, tolerance: Dp)Asserts that this value is equal to the given |
Cmn
|
inline Dp |
Dp.coerceAtLeast(minimumValue: Dp)Ensures that this value is not less than the specified |
Cmn
|
inline Dp |
Dp.coerceAtMost(maximumValue: Dp)Ensures that this value is not greater than the specified |
Cmn
|
inline Dp |
Ensures that this value lies in the specified range |
Cmn
|
inline Dp |
Dp.takeOrElse(block: () -> Dp)If this |
Cmn
|
inline Meter |
android
|
Extension properties |
||
|---|---|---|
Boolean |
Return |
Cmn
|
Boolean |
|
Cmn
|
Boolean |
|
Cmn
|
Public companion properties
Hairline
val Hairline: Dp
A dimension used to represent a hairline drawing element. Hairline elements take up no space, but will draw a single pixel, independent of the device's resolution and density.
Unspecified
val Unspecified: Dp
Constant that means unspecified Dp. Instead of comparing a Dp value to this constant, consider using isSpecified and isUnspecified instead.
Extension functions
assertIsEqualTo
fun Dp.assertIsEqualTo(expected: Dp, subject: String, tolerance: Dp = Dp(.5f)): Unit
Asserts that this value is equal to the given expected value.
Performs the comparison with the given tolerance or the default one if none is provided. It is recommended to use tolerance when comparing positions and size coming from the framework as there can be rounding operation performed by individual layouts so the values can be slightly off from the expected ones.
| Parameters | |
|---|---|
expected: Dp |
The expected value to which this one should be equal to. |
subject: String |
Used in the error message to identify which item this assertion failed on. |
tolerance: Dp = Dp(.5f) |
The tolerance within which the values should be treated as equal. |
| Throws | |
|---|---|
kotlin.AssertionError |
if comparison fails. |
coerceAtLeast
inline fun Dp.coerceAtLeast(minimumValue: Dp): Dp
Ensures that this value is not less than the specified minimumValue.
| Returns | |
|---|---|
Dp |
this value if it's greater than or equal to the |
coerceAtMost
inline fun Dp.coerceAtMost(maximumValue: Dp): Dp
Ensures that this value is not greater than the specified maximumValue.
| Returns | |
|---|---|
Dp |
this value if it's less than or equal to the |
coerceIn
inline fun Dp.coerceIn(minimumValue: Dp, maximumValue: Dp): Dp
Ensures that this value lies in the specified range minimumValue..maximumValue.
| Returns | |
|---|---|
Dp |
this value if it's in the range, or |