Home > Software engineering >  Not able to load Lottie animation from URL
Not able to load Lottie animation from URL

Time:09-15

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. Please help me on this.

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

CodePudding user response:

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