Neal Sanche
03/18/2021, 9:37 PMisVisible
is false, animate the alpha of a component infinitely between zero and one. If isVisible
is true, animate the current alpha up to 1.0f. Code in thread.Neal Sanche
03/18/2021, 9:39 PMDoris Liu
03/18/2021, 10:37 PMAnimatable
for this use case:
val alpha = remember { Animatable(0f) }
LaunchedEffect(isVisible) { // This restarts the job when isVisible changes
alpha.animateTo(1f)
if (!isVisible) { // Once at 1f, check whether repeating is needed.
alpha.animateTo(0f, animationSpec = infiniteRepeatable(...))
}
}
Doris Liu
03/18/2021, 10:40 PMinfiniteTransition
and transition
, the animation value won't be preserved during the isVisible
change. Hence there'll likely be a blink.Neal Sanche
03/18/2021, 10:50 PMvar started: Boolean by remember { mutableStateOf(true) }
// Launch a state change to make fake data visible
var isVisible by remember { mutableStateOf(false) }
LaunchedEffect(viewModel) {
started = false
delay(5000)
isVisible = !isVisible
}
val alpha: Float by animateFloatAsState(if (isVisible || started) 1.0f else 0.0f, animationSpec =
if (isVisible) tween(1000) else infiniteRepeatable(
animation = tween(1500),
repeatMode = RepeatMode.Reverse
))
Doris Liu
03/18/2021, 10:54 PMval alpha: Float by animateFloatAsState(if (isVisible || started) 1.0f else 0.0f...))
would work if you only flip isVisible
when it animates to 1f, otherwise it'd be infinitely animating between 0f and whatever the value is at the point of interruption. 🙂Neal Sanche
03/18/2021, 11:02 PMval alpha = remember { Animatable(0f) }
LaunchedEffect(isVisible) { // This restarts the job when isVisible changes
alpha.animateTo(1f, animationSpec = tween(1000))
if (!isVisible) { // Once at 1f, check whether repeating is needed.
alpha.animateTo(
0f, animationSpec = infiniteRepeatable(
animation = tween(1500),
repeatMode = RepeatMode.Reverse
)
)
}
}
Neal Sanche
03/18/2021, 11:04 PMNeal Sanche
03/18/2021, 11:06 PMDoris Liu
03/18/2021, 11:07 PMNeal Sanche
03/18/2021, 11:10 PMNeal Sanche
03/18/2021, 11:16 PM, animationSpec = tween(1000)
from the above code. I'll just leave it that way.Doris Liu
03/18/2021, 11:34 PMisVisible
was changed. The default spring animation will account for the velocity of the alpha change and make it smooth, whereas a tween
would animate the current value to the target in the same amount of time no matter what the delta is.
Alternatively, you could have the animation run between 0 and 1, and only check whether there should be another iteration after it arrives at 1f (didn't test this code but it should work):
val alpha = remember { Animatable(1f) }
LaunchedEffect(!isVisible || alpha.isRunning) { // This restarts the job when isVisible = true & alpha animation is finished
while (!isVisible) {
alpha.animateTo(0f, animationSpec = tween(1500))
alpha.animateTo(1f, animationSpec = tween(1500))
}
}
Neal Sanche
03/19/2021, 3:15 PMAnimatedVisibility
composables scattered around my code, and if this alpha animation is running a tween, those animations stutter and skip frames. Removing the tween
and having just the default spring animation in place seems to improve the animation of visibility for other elements. I'm not worried about this at the moment, I suspect the performance will be improved in future beta releases.