Not able to load Lottie animation from URL

I am trying to add Lottie animation in my view from URL. I am able to load from local asset folder. But When I try to load from URL it is not showing.

This is my code:

        String cacheKey ="LOTTIE_CACHE_KEY";
        mLottieDrawable = new LottieDrawable();
        mLottieDrawable.enableMergePathsForKitKatAndAbove(true);
        mLottieDrawable.setCallback(this);
        /*LottieResult<LottieComposition> result =
                LottieCompositionFactory.fromAssetSync(getContext().getApplicationContext(),
                        "woman_singer.json");
        mLottieDrawable.setComposition(result.getValue());*/

        String url = "https://assets5.lottiefiles.com/packages/lf20_GoeyCV7pi2.json";
        mLottieDrawable.setComposition(LottieCompositionFactory.fromUrlSync(getContext(), url, cacheKey).getValue());
        mLottieDrawable.setRepeatCount(LottieDrawable.INFINITE);

        mLottieDrawable.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                invalidate();
            }
        });
        mLottieDrawable.start();

Upvotes: 3

Views: 5304

Answers (2)

Mohamed AbdelraZek
Mohamed AbdelraZek

Reputation: 2809

You can use setAnimationFromUrl("url") passing the lottie json file as url, like so:

ivLottie.setAnimationFromUrl("https://assets5.lottiefiles.com/packages/lf20_GoeyCV7pi2.json")

xml:

<com.airbnb.lottie.LottieAnimationView
        android:id="@+id/ivLottie"
        android:layout_width="210dp"
        android:layout_height="160dp"  
        app:lottie_autoPlay="true"
        app:lottie_rawRes="@raw/box_gif" />

Upvotes: 3

Forgot to add Internet permission in my AndroidManifest. And also this code will be useful to someone who is working on Lottie. I searched in many sites. There is no proper example for Using LottieDrawable. So any one will get benefit by this code.

String cacheKey ="LOTTIE_CACHE_KEY";
    mLottieDrawable = new LottieDrawable();
    mLottieDrawable.enableMergePathsForKitKatAndAbove(true);
    mLottieDrawable.setCallback(this);
    /*LottieResult<LottieComposition> result =
            LottieCompositionFactory.fromAssetSync(getContext().getApplicationContext(),
                    "woman_singer.json");
    mLottieDrawable.setComposition(result.getValue());*/

    String url = "https://assets5.lottiefiles.com/packages/lf20_GoeyCV7pi2.json";
    mLottieDrawable.setComposition(LottieCompositionFactory.fromUrlSync(getContext(), url, cacheKey).getValue());
    mLottieDrawable.setRepeatCount(LottieDrawable.INFINITE);

    mLottieDrawable.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            invalidate();
        }
    });
    mLottieDrawable.start();

Upvotes: 2

Related Questions