简体   繁体   English

当数据来自使用json的url时如何在android中使用loading lottie动画

[英]how to use the loading lottie animation in android when data is coming from url using json

error 错误

Attempt to invoke virtual method 'void com.airbnb.lottie.LottieAnimationView.setAnimation(java.lang.String)' on a null object reference 尝试在空对象引用上调用虚拟方法'void com.airbnb.lottie.LottieAnimationView.setAnimation(java.lang.String)'

animationView.setAnimation("loading.json");
        animationView.playAnimation();

You should initialize the variable animationView 您应该初始化变量animationView

ie animationView = findViewById(R.id.animationView) animationView = findViewById(R.id.animationView)

I do not know which version of Lottie you're using. 我不知道您使用的是哪个版本的Lottie。 So implementing it like this: 这样实现它:

Use this for native files .json 将其用于本机文件.json

 animationView.setAnimation ("loading.json")

Use this for Urls.json 将此用于Urls.json

 animationView.setAnimationFromUrl ("https://gist.githubusercontent.com/Allanksr/d36c8a66ea24ec7df1ea422782fa7ae0/raw/bf1a65fc57b1db102893c80f0f3356e100e10a51/proxsensor.json")

Take a look: https://github.com/Allanksr/LottieFiles 看看: https//github.com/Allanksr/LottieFiles

Use the Lottie Animation using the following steps: 通过以下步骤使用Lottie Animation

Add the Gradle dependency to your app Gradle依赖项添加到您的应用程序

dependencies {  
implementation 'com.airbnb.android:lottie:2.7.0'
}

Add the view to Your Layout XML : 将视图添加到您的布局XML中

<com.airbnb.lottie.LottieAnimationView
       android:layout_width="wrap_conten"
       android:layout_height="wrap_conten"
       app:lottie_fileName="no_connection.json" //this is the JSON animation stored in assets folder
       app:lottie_autoPlay="true"
       app:lottie_loop="true"/>

In your Activity or Fragment init the view: 在您的“活动”或“片段”初始化视图中:

private LottieAnimationView animationView;
animationView = (LottieAnimationView) findViewById(R.id.animation_view);

if use form url : 如果使用表单网址:

 animationView.setAnimationFromUrl ("https://assets10.lottiefiles.com/datafiles/c56b08b89932c090a0948b53fd58427d/data.json")

Play/Cancel the Animation using: 使用以下方法播放/取消动画:

animationView.playAnimation();
animationView.cancelAnimation();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM