https://kotlinlang.org logo
#compose
Title
# compose
s

Se7eN

10/11/2020, 12:32 PM
I want to increase the progress of a
LinearProgressIndicator
over time. Like I want it to go from 0 to 100 in 3 seconds. How do I approach this?
l

Louis Pullen-Freilich [G]

10/11/2020, 2:38 PM
Copy code
val animatedFloat = animatedFloat(initVal = 0f)

LinearProgressIndicator(progress = animatedFloat.value)
And then in an
onClick
lambda or whenever you want to animate you can do:
animatedFloat.animateTo(1f, ProgressIndicatorConstants.DefaultProgressAnimationSpec)
(the default recommended animation) Or for 3 seconds you could do:
animatedFloat.animateTo(1f, tween(durationMillis = 3000))
s

Se7eN

10/11/2020, 2:48 PM
Great, thanks! Is there some way I can cancel the animation? Like if an event occurs it should stop animating further
Nvm found it.
stop()
c

caelum19

10/12/2020, 6:55 PM
Here's a modal progress bar, in case you want to stop the user from doing anything until you're done.
Copy code
for (i in 0 .. 100) {
    Thread.sleep(1000)
    LinearProgressIndicator(progress = i.toFloat())
}
Benefits: • No need to complicate your codebase with cancelling animations or allowing pesky users to break things while you're working • Long artificial waiting time gives the illusion that more is being done than really is • Crashing entire interface until done prevents user from switching to competing apps (🧌 )
s

Se7eN

10/14/2020, 11:02 AM
Power of compose 😳
😂 1
3 Views