TheGreatCornholio
TheGreatCornholio

Reputation: 1494

Android Kotlin - set animation end / finish listener

val anim = swipe.animate()
        .rotationBy((-30).toFloat())
        .setDuration(1000)
        .translationX((-swipe.left).toFloat())
        .setInterpolator(AccelerateDecelerateInterpolator())

anim.start()

I need an animation finish listener, I tried:

anim.setAnimationListener(object : Animation.AnimationListener {
    override fun onAnimationStart(p0: Animation?) {

    }

    override fun onAnimationRepeat(p0: Animation?) {

    }

    override fun onAnimationEnd(p0: Animation?) {

    }
})

but get this error

Unresolved reference: setAnimationListener

How to do this right?

Upvotes: 2

Views: 6947

Answers (1)

Son Truong
Son Truong

Reputation: 14183

Root cause

In ViewPropertyAnimator class, there is no method named setAnimationListener.

Solution 1

anim.withEndAction {
    // Your code logic goes here.
}

Solution 2

anim.setListener(object : Animator.AnimatorListener {
    override fun onAnimationRepeat(animation: Animator?) {}
    override fun onAnimationCancel(animation: Animator?) {}
    override fun onAnimationStart(animation: Animator?) {}

    override fun onAnimationEnd(animation: Animator?) {
        // Your code logic goes here.
    }
})

Note: Remember to cancel the anim if users leave the screen while animation.

swipe.animate().cancel()

Upvotes: 5

Related Questions