Hot questions for Using Lottie in kotlin

Question:

I can programatically start an Animation with lottie in Kotlin but I am struggling to create an AnimationListener. How exactly do I do that?

First, I tried it with an if-statement via the animation_view.progress but that did not work.

        textChanger.setOnClickListener{


                   animation_view.setAnimation("data.json")
                   animation_view.playAnimation()
                   animation_view.loop(false)
        }

I would like it to detect when the animation has finished so I can e.g. make a Toast. Are there any good lottie documentations for Kotlin?

Thanks for the help, just starting out with Android and Kotlin.


Answer:

You can check this.

Try using this code :

animation_view.addAnimatorListener(object:Animator.AnimatorListener {
    override fun onAnimationRepeat(animation: Animator?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }


    override fun onAnimationEnd(animation: Animator?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onAnimationCancel(animation: Animator?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onAnimationStart(animation: Animator?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

Question:

I found this Lottie Play/Pause button, but I don't know how to show Play state when my audio player paused, show Pause state and repeat with equalizer animation in it (see the lottie preview) when the player is playing. I've read the document but no example for this case. I don't have any knowledge about After Effect to control the animation too.


Answer:

According Lottie animation it has frames, so by manupulating this frames you can achieve what you want, sample

AnimationView play_pause = findViewById(R.id.play_pause);
play_pause.cancelAnimation();
play_pause.setMinFrame(90);
play_pause.setMaxFrame(175);

This code will repeat your animation in rage of 90-175 I tested it on you Lottie Play/Pause button, so on clicking to stop you must change MaxFrame to 210 and set loop to 1 after animation finished it will stop in play picture.

And this is whole code to achieve something like that

Boolean isPlaying = false;
play_pause = findViewById(R.id.play_pause);
play_pause.pauseAnimation();
play_pause.setMinFrame(60);
play_pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (isPlaying){

                play_pause.removeAllAnimatorListeners();
                play_pause.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {

                        if (valueAnimator.getAnimatedFraction() == 1){

                            play_pause.setMinFrame(60);
                            play_pause.pauseAnimation();

                        }
                    }
                });

                play_pause.setMinFrame(175);
                play_pause.setMaxFrame(210);
                isPlaying = false;
            } else {
                play_pause.setMaxFrame(175);
                play_pause.removeAllUpdateListeners();
                play_pause.addAnimatorListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {

                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {
                        play_pause.setMinFrame(90);
                    }
                });
                play_pause.resumeAnimation();
                isPlaying = true;
            }
        }
    });