https://kotlinlang.org logo
#compose
Title
# compose
m

Mehdi Haghgoo

11/03/2021, 2:42 PM
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?
b

Bryan Herbst

11/03/2021, 2:52 PM
Sounds like you are looking for animateDpAsState() 🙂
1
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.
1
m

Mehdi Haghgoo

11/03/2021, 2:59 PM
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

11/03/2021, 4:00 PM
You can always use
LocalDensity.current
to convert between dp and px
1
d

Doris Liu

11/03/2021, 6:53 PM
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. 🙂
❤️ 1
For the non-layout animations, such as
animate*AsState
,
Transition.animate*
or
Animatable
,
Dp
is supported out of the box.
❤️ 1
2 Views