Hot questions for Using Lottie in lottie android

Top 10 Java Open Source / Lottie / lottie android

Question:

I have an After Effects animation, super simple, of a square moving around (AE shape). I export the animation as a .json using Bodymovin, and add the json file using Lottie in my project. So far, so good.

The problem starts here --> during runtime, replace that "square" with an image I have in my project as well. Because this image may change, I can't add it statically to my AE animation, so need to dynamically add it during runtime. There's almost no information on how to do this in Android?


Answer:

Lottie has one XML attribute app:lottie_imageAssetsFolder, which can also be set at run-time: animationView.setImageAssetsFolder("images/");. with that set, one can reference images in the json. this is documented in-line; see the comments above line #599 and #630. this is also explained in the documentation (src/assets might not be an option, since it is not writable):

Sometimes, you don't have the images bundled with the device. You may do this to save space in your apk or if you downloaded the animation from the network. To handle this case, you can set an ImageAssetDelegate on your LottieAnimationView or LottieDrawable. The delegate will be called every time Lottie tries to render an image. It will pass the image name and ask you to return the bitmap. If you don't have it yet (if it's still downloading, for example) then just return null and Lottie will continue to ask on every frame until you return a non-null value.

animationView.setImageAssetDelegate(new ImageAssetDelegate() {
    @Override
    public Bitmap fetchBitmap(LottieImageAsset asset) {
        if (downloadedBitmap == null) {
            // We don't have it yet. Lottie will keep asking until we return a non-null bitmap.
           return null;
        }
        return downloadedBitmap;
    }
});

Question:

How can I use Lottie Animation in Android? I tried but the animation doesn't run. How to start the JSON animation? I want to use a custom loading animation instead of using regular circular progress bar.


Answer:

Use the Lottie Animation using the following steps:

Add the Gradle dependency to your app

dependencies {  
compile 'com.airbnb.android:lottie:2.2.0'
}

Add the view to Your Layout XML:

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/animation_view"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:lottie_fileName="preloader.json"//this is the JSON animation stored in assets folder
    app:lottie_loop="true"
    app:lottie_autoPlay="true" />

In your Activity or Fragment init the view:

private LottieAnimationView animationView;
animationView = (LottieAnimationView) findViewById(R.id.animation_view);

Play/Cancel the Animation using:

animationView.playAnimation();
animationView.cancelAnimation();

Question:

I'm developing an app with Lottie animations. One of lottie animation needs two image files(img_0.png, img1_.png).

For example,

lottie json file [ data.json ] : {"v":"5.1.7","fr":60,"ip":0,"op":120,"w":180,"h":200,"nm":"2","ddd":0,"assets":[{"id":"image_0","w":96,"h":96,"u":"images/","p":"img_0.png"},{"id":"image_1","w":180,"h":180,"u":"images/","p":"img_1.png"}],....

I can't prepare all images for LottieAnimationView in main/assets folder, so I'm using "setImageAssetDelegate" method to get multiple images from urls asynchronously.

This is my code.

        Glide.with(this)
            .asBitmap()
            .load("https://www.google.co.kr/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(final Bitmap resource, Transition<? super Bitmap> transition) {

                    LottieAnimationView lottie = findViewById(R.id.lottie);
                    lottie.setImageAssetDelegate(new ImageAssetDelegate() {
                        @Override
                        public Bitmap fetchBitmap(LottieImageAsset asset) {
                            return resource;
                        }
                    });
                    lottie.playAnimation();
                }
            });

But if i use "setImageAssetDelegate" method, I can put only one image to lottie. I want to put multiple different images in one lottie animation.

I dont want this. (the code above shows this animation.)

i need this with "setImageAssetDelegate" method.

Does anybody have any idea about this? Thanks.


Answer:

Fetching the images asynchronously will not work. The ImageAssetDelegate is meant to be a "provider" of the images that the animation needs when playing.

Its' method public Bitmap fetchBitmap(LottieImageAsset asset) will be called for each image in the animation while the animation is playing and it's your responsibility to provide the correct one, where the LottieImageAsset passed to that method will give you the details on which image is needed at that moment taken from the animation json file.

You can use Glide to fetch these images, need to keep in mind though that you'll have to fetch them synchonously and the animation will not continue playing until that image is loaded / provided by you.

An example:

LottieAnimationView lottie = findViewById(R.id.lottie);
lottie.setImageAssetDelegate(new ImageAssetDelegate() {
    @Override
    public Bitmap fetchBitmap(LottieImageAsset asset) {
        return Glide.loadSychronous("http://" + asset.getId + ".png");
    }
});
lottie.playAnimation(); 

Note: this is pseudo-code, you'll have to look up how to load images synchronously with Glide and you'll have to come up with a way to match the image id with the correct image url.

Another route would be using Glide to load all the images needed for the animation, save them (in memory or external storage) and only then start playing the animation; This might work for a small animation; for anything larger you'll run into trouble because a. the loading of all images takes too long or b. you run out of memory.