问题原因是多动图json文件切换动图效果变成静态图。
布局:
<com.airbnb.lottie.LottieAnimationView android:id="@+id/lottieAnimView" android:layout_width="@dimen/box_size_80" android:layout_height="@dimen/box_size_80" android:layout_gravity="center" android:layout_margin="@dimen/layout_padding" app:lottie_autoPlay="false" app:lottie_loop="true" />
Dialog:
public class PermissionLoadingDialog extends Dialog implements DialogInterface.OnDismissListener { private Context mContext; private LottieAnimationView lottieView; private TextView tvId; private String json = ""; public PermissionLoadingDialog(@NonNull Context context, int themeResId) { super(context, themeResId); this.mContext = context; } public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } public void test(String id) { if (isMobileConnected(mContext)) { tvId.setText(id + "/5"); } else { tvId.setText(id + "/4"); } if (lottieView.isAnimating()) { lottieView.cancelAnimation(); } if (id.equals("1")) { json = "wifi.json"; } if (id.equals("2")) { if (isMobileConnected(mContext)) { json = "cellular_network.json"; } else { json = "bluetooth.json"; } } if (id.equals("3")) { if (isMobileConnected(mContext)) { json = "bluetooth.json"; } else { json = "gps.json"; } } if (id.equals("4")) { if (isMobileConnected(mContext)) { json = "gps.json"; } else { json = "battery.json"; } } if (isMobileConnected(mContext)) { if (id.equals("5")) { json = "battery.json"; } } LottieComposition.Factory.fromAssetFileName(mContext, json, composition -> { lottieView.setComposition(composition); lottieView.setAnimation(json); lottieView.playAnimation(); }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setDimAmount(0.2f); View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_permission_loading_anim, null); setContentView(inflate); lottieView = inflate.findViewById(R.id.lottieAnimView); tvId = inflate.findViewById(R.id.tv_id); setOnDismissListener(this); this.setCanceledOnTouchOutside(false); this.setCancelable(false); } @Override public void show() { super.show(); lottieView.playAnimation(); } @Override public void onDismiss(DialogInterface dialog) { if (lottieView != null) { lottieView.pauseAnimation(); } } public static PermissionLoadingDialog showDiaolog(Context context) { PermissionLoadingDialog loadingDialog = new PermissionLoadingDialog(context, R.style.AwakenDialog); loadingDialog.show(); return loadingDialog; } }