我正在尝试从 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/