Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when a Lottie animation is completed?

I have a fragment, here is the onCreateView method:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          // Inflate the layout for this fragment     mView = inflater.inflate(R.layout.fragment_added_to_cart_anim, container, false);     ButterKnife.bind(this, mView);      mAddedToCartAnimation.setAnimation("checked_done_.json");     mAddedToCartAnimation.loop(false);     mAddedToCartAnimation.playAnimation();      // Remove fragment when the animation is finished.      return mView;  } 

I need to remove the fragment using getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); when the lottie animation has ended. If I understand correctly when the isAnimating() Lottie method returns false the animation has ended and since in my configuration the animation does not loop this is when I should remove the current fragment. But I can't just use an if statement since when it is executed the animation may still be going.

I need a way to remove the fragment when the Lottie animation ends, how do I do this?

like image 844
Tom Finet Avatar asked Sep 12 '17 15:09

Tom Finet


People also ask

How do you test a Lottie animation?

Start LottieCloud, hit the browse button or just drag your json files into the browser. Get your LottieCloud ID by clicking the user icon on the top right. Open the LottieCloudPlayer app and scan or enter your ID. That's it.

How do Lottie animations work?

Lottie is an animation rendering library open-sourced by Airbnb. With Android, it also supports iOS, React Native, Windows, and Web platforms. Lottie currently supports rendering and playing After Effects animations. Lottie uses the JSON data exported by bodymovin, an After Effects plug-in as the animation data source.

Are Lottie animations copyright free?

All free Lottie animations are under Creative Commons License, so go ahead and use them without hesitation.

How can I increase my Lottie animation speed?

You can use the SetSpeed action to a value between 1 and higher to make it faster. So 1 is normal speed, and 2 is faster and 3 is even faster. Any value between 0 and 1 will slow down the animation speed.


1 Answers

This code works for me:

mAddedToCartAnimation.addAnimatorListener(new Animator.AnimatorListener() {         @Override         public void onAnimationStart(Animator animation) {             Log.e("Animation:","start");         }          @Override         public void onAnimationEnd(Animator animation) {             Log.e("Animation:","end");             //Your code for remove the fragment             try {                 getActivity().getSupportFragmentManager()                       .beginTransaction().remove(this).commit();             } catch(Exception ex) {                 ex.toString();             }         }          @Override         public void onAnimationCancel(Animator animation) {             Log.e("Animation:","cancel");         }          @Override         public void onAnimationRepeat(Animator animation) {             Log.e("Animation:","repeat");         }     }); 

I hope this solve your problem :)

like image 88
Chefes Avatar answered Sep 28 '22 05:09

Chefes