📜  android lottie auto hide at the end - 不管是什么(1)

📅  最后修改于: 2023-12-03 14:39:08.104000             🧑  作者: Mango

Android Lottie Auto Hide at the End

Introduction

In Android development, Lottie is a powerful library that allows you to easily add animations to your application. One common requirement is to auto hide an animation at the end without any manual intervention. In this tutorial, we will explore how to achieve this using Lottie in Android.

Prerequisites
  • Basic knowledge of Android development
  • Familiarity with Lottie library
Steps
  1. Add Lottie dependency - To use Lottie in your project, add the following dependency to your app-level build.gradle file.
dependencies {
    implementation 'com.airbnb.android:lottie:3.7.0'
}
  1. Add Lottie animation file - Obtain or create a Lottie animation file in JSON format. You can find pre-built animations from websites like LottieFiles or create custom ones using Adobe After Effects and exporting as JSON.

  2. Create a LottieAnimationView - In your activity layout XML file, add a LottieAnimationView that will display your animation.

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/animation_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:lottie_fileName="your_animation.json"
    app:lottie_autoPlay="true"
    app:lottie_loop="false" />
  1. Auto hide animation at the end - In your activity or fragment, find the LottieAnimationView by its ID and set an animation listener to detect when the animation ends.
LottieAnimationView animationView = findViewById(R.id.animation_view);
animationView.addAnimatorListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {
        // Animation started
    }
    
    @Override
    public void onAnimationEnd(Animator animation) {
        // Animation ended, hide the view here
        animationView.setVisibility(View.GONE);
    }
    
    @Override
    public void onAnimationCancel(Animator animation) {
        // Animation cancelled
    }
    
    @Override
    public void onAnimationRepeat(Animator animation) {
        // Animation repeated
    }
});
  1. Enjoy auto-hide feature - Now, whenever the animation reaches its end, the LottieAnimationView will automatically be hidden by setting its visibility to GONE.
Conclusion

By following the above steps, you can easily achieve an auto-hide feature for Lottie animations in your Android application. Lottie provides a seamless way to integrate captivating animations into your app while allowing control over their lifecycle. Happy animating with Lottie!