How can I translate a composable's visible positio...
# compose
r
How can I translate a composable's visible position without snapping to whole pixels -- i.e. fractional offsets. The pixel snapping causes animation settling to be jittery. Here's an example - you can see that the red box does not move smoothly:
Copy code
val offsetY by rememberInfiniteTransition().animateFloat(
    initialValue = -3f,
    targetValue = 3f,
    animationSpec = infiniteRepeatable(
        animation = tween(easing = EaseInOutCubic, durationMillis = 2500),
        repeatMode = RepeatMode.Reverse))

Box(modifier = Modifier.size(128.dp).padding(32.dp)) {
    Box(modifier = Modifier.size(64.dp).offset(y = offsetY.dp).background(Color.Red))
}
s
Use Modifier.graphicsLayer { translationX = ... translationY = .. } instead of Modifier.offset()
👏 1
To ensure your app does as little as possible while animating, choose the lambda version of a
Modifier
where possible. This skips recomposition and performs the animation outside of the composition phase, otherwise use
Modifier.graphicsLayer{ }
, as this modifier always runs in the draw phase. For more information on this, see the deferring reads section in the performance documentation.
👍 1
r
More importantly remember that layout is done in integer pixels, but drawing can be done fractionally. Hence the suggested use of graphicsLayer here.
👍 1
r
Thanks for the advice re: graphicsLayer + translation. I'm finding the same behaviour there -- even if I specify a fractional translation, it gets drawn at an integer offset
Errr, yet somehow when I just re-attempted this, it is drawn fractionally. Oops, guess I screwed something up earlier. Thank you 🙂