Search code examples
androidanimationlottie

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();

Solution

  • 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();