Lucas
Lucas

Reputation: 87

Delay repeat animation in reverse mode

I'm trying to add some delay to to the repeat animation, but startDelay isn't work. It looks like it wonly works when the animation is played for the first time.

val path = Path().apply {
    moveTo(imageView.x, imageView.y)
    lineTo(x.toFloat(), y.toFloat())
}
ObjectAnimator.ofFloat(imageView, View.X, View.Y, path).apply {
    duration = Random.nextLong(500, 1000)
    startDelay = 1000
    doOnEnd {
    startDelay = 3000
    } 
start()
}

I also tried to use Timer and Handler().postDelayed but it doesn't even repeat:

val path = Path().apply {
    moveTo(imageView.x, imageView.y)
    lineTo(x.toFloat(), y.toFloat())
}
ObjectAnimator.ofFloat(imageView, View.X, View.Y, path).apply {
    duration = Random.nextLong(500, 1000)
    startDelay = 1000
    doOnStart {
        Timer().schedule(object : TimerTask() {
            override fun run() {
                repeatCount = 1
                repeatMode = ValueAnimator.REVERSE
             }
        }, 3000)
    }
 start()
}

How can I achieve the repeat in reverse mode delayed?

Upvotes: 0

Views: 779

Answers (1)

Farhad Navayazdan
Farhad Navayazdan

Reputation: 1121

You can use this code for simulating the delay for the animation.

The pause/delay/resume will do the trick for you.

val path = Path().apply {
    moveTo(imageView.x, imageView.y)
    lineTo(x.toFloat(), y.toFloat())
}

val delayBetweenRepeats = 2_000L

ObjectAnimator.ofFloat(imageView, View.X, View.Y, path).apply {
    duration = Random.nextLong(500, 1000)
    startDelay = 1000
    repeatCount = 5
    repeatMode = ValueAnimator.REVERSE
    doOnRepeat {
        pause()
        Timer().schedule(object : TimerTask() {
            override fun run() {
                runOnUiThread { resume() }
            }
        }, delayBetweenRepeats)
    }
    start()
}

Upvotes: 2

Related Questions