Open In App

How to add Lottie Animation in an Android app

This article is about enhancing the User Interface of the mobile application by adding Lottie animation files into our mobile app. The Lottie animations are free to use vector animation files. These animation files can be found at the official site here. Many famous applications use this such as Uber, Netflix, Google, Airbnb, Shopify, etc.
Below is an example of Lottie Animation:
 

Dynamic properties of Lottie Animations



Advantages of Lottie

Approach: 
Step 1: Add this dependency into the App level gradle module of the project and then sync the gradle with the project. This library enables us to use Lottie’s animations: 
 






// Lottie dependency
def lottieVersion = "3.4.0" implementation "com.airbnb.android:lottie:$lottieVersion"

Step 2: Choose any animation from here and download the JSON file of animation 
 

Downloading the JSON animation file

Step 3: Now include the XML code into the layout file where you want to show the animation. Here the file name for JSON file is assumed to be animation. Different methods through which Lottie can take JSON files

Below is the XML code: 
 




<com.airbnb.lottie.LottieAnimationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/animation_view"
            app:lottie_rawRes="@raw/animation"
            app:lottie_autoPlay="true"
            app:lottie_loop="true"/>

Step 4:(optional) Adding controls to animation: 
 




// Custom animation speed or duration.
ValueAnimator animator
    = ValueAnimator.ofFloat(0f, 1f);
animator
    .addUpdateListener(animation -> {
        animationView
            .setProgress(
                animation
                    .getAnimatedValue());
    });
animator.start();

 




// Declaring the animation view
LottieAnimationView animationView
    = findViewById(R.id.animationView);
animationView
    .addAnimatorUpdateListener(
        (animation) -> {
            // Do something.
        });
animationView
    .playAnimation();
 
if (animationView.isAnimating()) {
    // Do something.
}

Output: 
 

Reference: https://github.com/airbnb/lottie-android
 


Article Tags :