Thread
#compose
    Mehdi Haghgoo

    Mehdi Haghgoo

    10 months ago
    Why do most animation APIs work pixel-based? and given the fact that units in Android are often given in d.p, what is the best practice for creating accurate animations?
    Bryan Herbst

    Bryan Herbst

    10 months ago
    Sounds like you are looking for animateDpAsState() 🙂
    Animations at some point need to work in pixels, because fundamentally that is what we are drawing on screen. There are helpers like
    animateDpAsState()
    that do some of that conversion for us though.
    Mehdi Haghgoo

    Mehdi Haghgoo

    10 months ago
    animateDpAsState is nice, but imagine the time we are working with gesture detection APIs like draggable or animateScrollTo, which take pixel parameters.
    f

    Felix Schütz

    10 months ago
    You can always use
    LocalDensity.current
    to convert between dp and px
    Doris Liu

    Doris Liu

    10 months ago
    Why do most animation APIs work pixel-based?
    I'm assuming this is referring specifically to layout animations. Layout sizes can be defined with
    dp
    or pixel (for custom layouts). Therefore if we had to pick one unit to support, pixel would be more accurate.
    and given the fact that units in Android are often given in d.p, what is the best practice for creating accurate animations?
    Using
    LocalDensity.current
    for dp <-> px conversion would be my recommendation as well. 🙂
    For the non-layout animations, such as
    animate*AsState
    ,
    Transition.animate*
    or
    Animatable
    ,
    Dp
    is supported out of the box.