'Add Listener to ObjectAnimator using Lambda

I have set-up this objectAnimator that I need to add a listener to:

val animator = ObjectAnimator.ofFloat(star, View.TRANSLATION_X, 200f)
                    .apply {
                        repeatCount = 1
                        repeatMode = ObjectAnimator.REVERSE
                        start()

                    }

I know how to use the anonymous listner like below:

 animator.addListener(object : AnimatorListenerAdapter() {

        override fun onAnimationStart(animation: Animator?) {
            super.onAnimationStart(animation)
            //callback 1 - do something
            
        } 
          override fun onAnimationEnd(animation: Animator?) {
            super.onAnimationEnd(animation)
            //callback 2 - do something
            
        }

What I now want is to experiment with the lambda version but I am stuck on how to call the listener's callbacks: My code looks like this

animator.addListener {
        onStart(it) {
            //get errors with this code
        }

Please help.



Solution 1:[1]

I think you can do it like that. You can pick the animator object by it.

animator.addListener(
            onEnd = { animator: Animator -> animator.start() },
            onStart = {},
            onCancel = {},
            onRepeat = {})

Solution 2:[2]

Sample in Kotlin :

val sizeAnimation = ValueAnimator.ofFloat(lineWidth * 2)
        sizeAnimation
            .apply {
                addUpdateListener {
                    annimation.strokeWidth = it.animatedValue as Float
                    myView.postInvalidateOnAnimation()
                }
                repeatCount = 5
                repeatMode = ValueAnimator.REVERSE
                interpolator = LinearInterpolator()
                duration = 1000L
                start()
            }.doOnEnd {
                // What to do in the end? // sizeAnimation.start()
            }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Thiago