android - 无法从 URL 加载 Lottie 动画

标签 android animation lottie

我正在尝试从 URL 在我的 View 中添加 Lottie 动画。我可以从本地资源文件夹加载。但是当我尝试从 url 加载时,它没有显示。请帮我解决这个问题。

这是我的代码:

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

最佳答案

忘记在我的 AndroidManifest 中添加互联网权限。而且这段代码对于从事 Lottie 工作的人也很有用。我在很多网站上进行了搜索。没有使用 LottieDrawable 的正确示例。因此任何人都会从这段代码中受益。

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

关于android - 无法从 URL 加载 Lottie 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73657802/

相关文章:

android - 单击 RecyclerView 外部以隐藏 RecyclerView 行中的 ChildView

android - achartengine - 倾斜标签位置

iphone - iPhone 应用首次加载查询时的动画

objective-c - NSTimer 问题

android - 如何在按钮内使用 Lottie Animation 而不是圆形进度条

android - 洛蒂和 RecyclerView

android - 在 Android SQLite 中重置自动增量

android - 恢复 sqlite 数据库后,旧文件保持 Activity 状态??

javascript - 淡入或淡出取决于事件类别,淡入有效,淡出则不然

javascript - 为什么没有lottie规范?