Search code examples
androidjsonanimationpaddinglottie

Lottie animation padding


I have a question for the ones of you who have experience using lottie json files. I am not so experienced using lottie so I thought maybe I am missing some obvious knowledge. When I render my animation into a view, the animation object are placed in the center of the view like that

 _____________________________________________
|                                             |
|        [animated object]                    |
|_____________________________________________|

Is there a way I can modify the json file to make the animated objects fit the whole view like that:

     _____________________________________________
    |                                             |
    | [a n i m a t e d         o b j e c t s     ]|
    |_____________________________________________|

I have tried setting the view in the xml like that:

android:scaleType="centerCrop"
android:adjustViewBounds="true"

but it didn't work
I also tried to set a greater scale:

app:lottie_scale="2" 

but I had no success. Thank you for your time!


Solution

  • Using "LottieDrawable" and "LottieComposition" you can set scale easily. Bellow code is 100% working for me.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:lottie_loop="true"
        app:lottie_autoPlay="true"/>
    
    </LinearLayout>
    

    and Java part

    LottieAnimationView animationView = findViewById(R.id.animation_view);
    
    LottieDrawable drawable = new LottieDrawable();
    
        LottieComposition.Factory.fromAssetFileName(this, "anim_1.json",(composition -> {
            drawable.setComposition(composition);
            drawable.playAnimation();
            drawable.setScale(3);
            animationView.setImageDrawable(drawable);
    
        }));