https://kotlinlang.org logo
Title
m

myanmarking

04/18/2022, 10:32 AM
Is there a better way of doing this animation?
LaunchedEffect(key1 = Unit) {
snapshotFlow { animationStage }
.filterNotNull()
.collectLatest { stage ->
when (stage) {
is stageA -> {
try {
while (isActive) {
animScale.animateTo(1.05f, tween(500))
animScale.animateTo(1f, tween(500))
}
} catch (e: Exception) {
if (e is CancellationException) {
launch {
withContext(NonCancellable) {
animScale.animateTo(1f, tween(500))
}
}
throw e
}
}
}
is stageB -> ...
is stageC -> ...
}
}
}
Basically, its a scaleIn scaleOut animation that repeats forever, until another stage comes in, in that case, it should go to it’s initial place, without suspending so other stages can happen while the stageA animation is resting to its original position
currently it is working as i want it, but i feel there must be already an api for this
c

Chris Sinco [G]

04/18/2022, 5:33 PM
Do you have a mock-up or video of what you are trying to build? I ask because of the comment “until another stage comes in, in that case, it should go to it's initial place”
Otherwise, I think you can achieve what you want with rememberInfiniteTransition
m

myanmarking

04/18/2022, 5:39 PM
Imagine scale and drag at the same time. Initially, its scalling up and down infinitivelly. When the user touches to drag, the scale should be cancelled, but not mid-scale. It should cancel after going back to 1f. But also, it cannot suspend in that case, otherwise it wont drag in that period
Its a drag animation, but until the user touches it, its scaling up 1.05f back to 1f infinitivelly. When user starts to drag, it will move, but also the scale will rest at 1f, not mid-scale
c

Chris Sinco [G]

04/18/2022, 5:54 PM
I see. I think it'd still be useful to provide a video of what you want as I'm not sure why you wouldn't want to cancel or suspend the animation on the drag event, otherwise the animation might not feel responsive to touch.
Since you have it working, and you don't want to suspend immediately on user interaction, I'm not sure there's a direct API for what you need
m

myanmarking

04/18/2022, 6:00 PM
Its the other way around
The drag will start immediatly
If i remove the launch in stageA, the drag will be delayed. This way, it will start imediatly, but at the same time it will settle the scale at 1f