Why do most animation APIs work pixel-based? and g...
# compose
m
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
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
animateDpAsState is nice, but imagine the time we are working with gesture detection APIs like draggable or animateScrollTo, which take pixel parameters.
f
You can always use
LocalDensity.current
to convert between dp and px
1
d
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