I have an animation to wiggle a button. I want it repeat infinitely with a delay between each loop.
I tried using doOnEnd
with a startDelay
and it worked well, but eventually caused a StackOverflowError.
Using doOnEnd with startDelay (works but causes SO error):
val animator = ObjectAnimator
.ofFloat(premiumButton, "translationX", 0F, 25F, -25F, 25F, -25F, 15F, -15F, 6F, -6F, 0F)
.setDuration(2000L)
animator.doOnEnd {
it.startDelay = 10000
it.start()
}
animator.start()
Next I tried using an AnimatorSet
but my animations are different durations and so it just jiggles continuously without a delay between jiggles no matter what configuration/properties I use (since the jiggle is the shorter animation).
Using AnimatorSet (didn't work because animations are different durations):
val animatorJiggle = ObjectAnimator
.ofFloat(myButton, "translationX", 0F, 25F, -25F, 25F, -25F, 15F, -15F, 6F, -6F, 0F)
.setDuration(2000)
.apply { repeatCount = ObjectAnimator.INFINITE }
val animatorDelay = ObjectAnimator
.ofFloat(myButton, "translationX", 0F)
.setDuration(10000)
.apply { repeatCount = ObjectAnimator.INFINITE }
AnimatorSet().apply {
playSequentially(animatorJiggle, animatorDelay)
// repeat(ObjectAnimator.INFINITE) { }
start()
}
How do I get an animation to loop with a delay between each loop without using the first solution (since it causes an SO error)?
CodePudding user response:
I found a solution using the the pause
and resume
functions of the ObjectAnimator
with coroutines. Basically, I pause the animations, wait for the delay, then resume. I don't know if this will cause a StackOverflow error, but I don't think so.
Repeat animation with a delay:
private fun ObjectAnimator.repeatWithDelay(delay: Long, lifecycleScope: LifecycleCoroutineScope) {
repeatCount = ObjectAnimator.INFINITE
doOnRepeat {
pause() // pause animator
lifecycleScope.launchWhenResumed {
delay(delay)
resume() // resume animator
}
}
}
Using it (inside an activity):
val jiggleAnimator = ObjectAnimator
.ofFloat(myButton, "translationX", 0F, 25F, -25F, 25F, -25F, 15F, -15F, 6F, -6F, 0F)
.setDuration(2000)
jiggleAnimator.repeatWithDelay(10000, [email protected])
jiggleAnimator.start()
You could also use a timer or a runnable, but I chose to use coroutines because it's safer (you can make sure you're not trying access the UI or variables of an activity after its been killed).
CodePudding user response:
This location offers a variety of party areas that can be tailored to your celebration's demands and specifications. For your convenience, you can use the areas separately or collectively. It's well-suited for celebrations like birthday parties, anniversaries, corporate meetings, seminars, pre-wedding events, and weddings. The banquet halls have modern décor and are completely air-conditioned
wedding banquets in alipur
click-https://weddingbanquets.in/mithas-motel-wedding-hall