Thread
#compose
    Ian Warwick

    Ian Warwick

    2 years ago
    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()
    ...
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    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:
    Text(
      text = "Hello world!",
      // apply theme as per requirement                    
     style = TextStyle(
        color = Color.Blue,
        fontSize = +withDensity { 50.dp.toPx().value }), 
        textAlign = TextAlign.Center
    )
    Ian Warwick

    Ian Warwick

    2 years ago
    nice thanks!
    For some reason I had to also pass density as first required argument
    val density = Density(+ambient(ContextAmbient))
        val widthPx = +withDensity(density) { ovalWidth.toPx().value }
        val heightPx = +withDensity(density) { ovalHeight.toPx().value }
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    weird… probably they’re in the middle of changing the api? ¯_(ツ)_/¯
    Ian Warwick

    Ian Warwick

    2 years ago
    yep probably updated in alpha03
    matvei

    matvei

    2 years ago
    dev03* 🙂
    Ian Warwick

    Ian Warwick

    2 years ago
    aha yes apologies 🙂