[英]How to add a Lottie animation in Java programatically
I tried to add Lottie Animation to Java (Android) programmatically, but I keep failing.我试图以编程方式将 Lottie Animation 添加到 Java (Android),但我一直失败。 I will show my code below I need to change from drawable to Lottie.
我将在下面显示我的代码,我需要将可绘制对象更改为 Lottie。
This is the code for drawables:这是可绘制对象的代码:
if (status.equals("connect")) {
vpnBtn.setBackgroundResource(R.drawable.but_connect_light);
logTv.setTextColor(getResources().getColor(R.color.font_color));
} else if (status.equals("connecting")) {
vpnBtn.setBackgroundResource(R.drawable.but_connecting_dark);
logTv.setTextColor(getResources().getColor(R.color.font_color));
} else if (status.equals("connected")) {
vpnBtn.setBackgroundResource(R.drawable.but_disconnect_dark);
logTv.setTextColor(getResources().getColor(R.color.purple));
} else if (status.equals("failed")) {
vpnBtn.setBackgroundResource(R.drawable.but_connect_dark);
}
This is the xml relating above这是上面相关的xml
<ImageView
android:id="@+id/vpnBtn"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:tag="1"
android:layout_width="140dp"
android:layout_height="140dp"
android:background="@drawable/but_connect_light"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"/>
First Use this dependency :首先使用这个依赖:
implementation 'com.airbnb.android:lottie:3.6.1'
Add LottieAnimationView
in xml:在 xml 中添加
LottieAnimationView
:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_fileName="loading.json"
app:lottie_loop="true"
app:lottie_speed="1" />
Change animation according to your use:根据您的用途更改 animation:
animation.setAnimation("loading.json");
Note : Add your lottie animation json file in asset folder.注意:在资产文件夹中添加您的乐天 animation json 文件。
At last i achieved this with the below solution最后我通过以下解决方案实现了这一点
String vpnsBtn;
if (check.equals("night")){
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connectingg.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connected.json");
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
break;
}
}else {
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connectingg.json");
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("connected.json");
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = ("disconnected.json");
break;
}
}
LottieAnimationView animationView = findViewById(R.id.vpnBtn);
animationView.setImageAssetsFolder("vpnconnect");
animationView.setAnimation(vpnsBtn);
animationView.loop(true);
animationView.playAnimation();
Hope this will help other who want to use dynamic button with Lottie on Android.希望这将有助于其他想要在 Android 上使用 Lottie 的动态按钮的人。
For using Lottie I did these:为了使用 Lottie,我做了这些:
In build.gradle(:app)
add this and sync:在
build.gradle(:app)
添加这个并同步:
implementation 'com.airbnb.android:lottie:$lottieVersion'
then, add assets
file.然后,添加
assets
文件。 Then, add the JSON files there.然后,在此处添加 JSON 文件。 Then, go to
activity_main.xml
file, add:然后,转到
activity_main.xml
文件,添加:
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lav_actionBar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:lottie_autoPlay="true"
app:lottie_fileName="circle-load.json" --> add your JSON file name
app:lottie_loop="true" />
The above solutions didn't work for me.上述解决方案对我不起作用。 It crashes the app.
它使应用程序崩溃。
try using this尝试使用这个
animationView.setAnimation(R.raw.your_lottie_file)
or, adding it to the answer from Infinite或者,将其添加到 Infinite 的答案中
`String vpnsBtn; `字符串 vpnsBtn;
if (check.equals("night")){ if (check.equals("night")){
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connectingg;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connected
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
break;
}
}else {
switch (status) {
case "connect":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connecting":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connectingg;;
logTv.setTextColor(getResources().getColor(R.color.font_color));
break;
case "connected":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.connected;;
logTv.setTextColor(getResources().getColor(R.color.purple));
break;
case "failed":
vpnBtn.setVisibility(View.VISIBLE);
vpnsBtn = R.raw.disconnected;
break;
}
}
LottieAnimationView animationView = findViewById(R.id.vpnBtn);
animationView.setImageAssetsFolder("vpnconnect");
animationView.setAnimation(vpnsBtn);
animationView.loop(true);
animationView.playAnimation();`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.