https://kotlinlang.org logo
i

Ian Warwick

01/08/2020, 9:27 PM
how to convert
Dp
to
Px
is there any special helper yet with compose APIs ? 🙂
Docs say there is a
toPx
but I cannot find it in
Dp
however there is an
IntPx.toPx()
...
z

Zach Klippenstein (he/him) [MOD]

01/08/2020, 10:50 PM
You need to know the density of the display to perform the conversion. Use
withDensity
to get access to the
toPx
function. Example from https://medium.com/mindful-engineering/saying-hello-to-jetpack-compose-a96d9e9c4f77:
Copy code
Text(
  text = "Hello world!",
  // apply theme as per requirement                    
 style = TextStyle(
    color = Color.Blue,
    fontSize = +withDensity { 50.dp.toPx().value }), 
    textAlign = TextAlign.Center
)
i

Ian Warwick

01/08/2020, 11:07 PM
nice thanks!
For some reason I had to also pass density as first required argument
Copy code
val density = Density(+ambient(ContextAmbient))
    val widthPx = +withDensity(density) { ovalWidth.toPx().value }
    val heightPx = +withDensity(density) { ovalHeight.toPx().value }
z

Zach Klippenstein (he/him) [MOD]

01/09/2020, 1:02 AM
weird… probably they’re in the middle of changing the api? ¯\_(ツ)_/¯
i

Ian Warwick

01/09/2020, 8:36 AM
yep probably updated in alpha03
m

matvei

01/09/2020, 12:24 PM
dev03* 🙂
i

Ian Warwick

01/09/2020, 12:25 PM
aha yes apologies 🙂
25 Views