Adriano Celentano
02/21/2021, 9:52 AMLaunchedEffect(key1 = Unit, block = {
while (isActive) {
delay(50)
horizontalAnimation.value = horizontalAnimation.value + 2.dp
}
})
Adriano Celentano
02/21/2021, 9:53 AMval transition = rememberInfiniteTransition()
val amplitudeAnimation = transition.animateFloat(
initialValue = -1f, targetValue = 1f, animationSpec = InfiniteRepeatableSpec(
tween(3000),
repeatMode = RepeatMode.Reverse
)
)
Adam Powell
02/21/2021, 3:03 PMwithFrame[Nanos|Millis] { frameTime ->
is a better alternative than delay,
then you can calculate the amount to animate by based on how much time elapsed since the previous frame. delay
will be janky and not align with frames.Adam Powell
02/21/2021, 3:04 PMjim
02/21/2021, 6:20 PMAdam Powell
02/21/2021, 6:46 PMjim
02/21/2021, 6:50 PMDoris Liu
02/21/2021, 6:55 PMwithFrame[Nanos|Millis]
makes total sense here for calculating the offset, as you probably want a constant velocity. One thing to watch out for is the rounding error, as compounding rounding error on each frame may produce a subtle drift. Alternatively, an equally viable option would be to have an animation in a while-loop to take care of that calculation/rounding for you. It could be something like:
LaunchedEffect(...) {
val horizontalAnimation = AnimationState(Dp.VectorConverter, 0.dp)
while(isActive) {
horizontalAnimation.animateTo(
horizontalAnimation.value + 2.dp,
tween(50, easing = LinearEasing),
sequentialAnimation = true
) {
// Update something with the animation value
}
}
}
Doris Liu
02/21/2021, 6:58 PMdelay
: if you want to do something once per interval, and the interval is long enough (say 5 seconds) that being off by one frame (from not aligning with frame time) is not a concern. Seems like delay
would be a convenient way to achieve that.jim
02/21/2021, 7:00 PMDoris Liu
02/21/2021, 7:30 PMdelay
, assuming you use the TestCoroutineDispatcher
or implement DelayController
correctly: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-delay-controller/advance-time-by.htmlAdriano Celentano
02/22/2021, 7:55 AM