如何知道 Lottie 动画何时完成?

How to know when a Lottie animation is completed?

我有一个片段,这里是 onCreateView 方法:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    // Inflate the layout for this fragment
    mView = inflater.inflate(R.layout.fragment_added_to_cart_anim, container, false);
    ButterKnife.bind(this, mView);

    mAddedToCartAnimation.setAnimation("checked_done_.json");
    mAddedToCartAnimation.loop(false);
    mAddedToCartAnimation.playAnimation();

    // Remove fragment when the animation is finished.

    return mView;

}

我需要在 lottie 动画结束时使用 getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit(); 删除片段。如果我在 isAnimating() Lottie 方法 returns false 时正确理解动画已经结束,并且由于在我的配置中动画不会循环,这就是我应该删除当前片段的时候。但我不能只使用 if 语句,因为执行它时动画可能仍在运行。

我需要一种在 Lottie 动画结束时删除片段的方法,我该怎么做?

这段代码对我有用:

mAddedToCartAnimation.addAnimatorListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            Log.e("Animation:","start");
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Log.e("Animation:","end");
            //Your code for remove the fragment
            try {
                getActivity().getSupportFragmentManager()
                      .beginTransaction().remove(this).commit();
            } catch(Exception ex) {
                ex.toString();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            Log.e("Animation:","cancel");
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
            Log.e("Animation:","repeat");
        }
    });

希望这能解决您的问题:)

Kotlin 动画代码完成:

successAnimation.addAnimatorListener(object : Animator.AnimatorListener{
            override fun onAnimationRepeat(animation: Animator?) {
            }

            override fun onAnimationEnd(animation: Animator?) {
                //Add your code here for animation end
            }

            override fun onAnimationCancel(animation: Animator?) {
            }

            override fun onAnimationStart(animation: Animator?) {
            }

        })

在 Kotlin 中,你可以这样:

val lotWelcome=findViewById<LottieAnimationView>(R.id.lot_1) 
   
lotWelcome.addAnimatorListener(object : Animator.AnimatorListener {
       override fun onAnimationStart(animation: Animator) {
           Log.e("Animation:", "start")
       }
   override fun onAnimationEnd(animation: Animator) {
       Log.e("Animation:", "end")
       //Ex: here the layout is removed!
       firstScreen.visibility= View.GONE
   }

   override fun onAnimationCancel(animation: Animator) {
       Log.e("Animation:", "cancel")
   }

   override fun onAnimationRepeat(animation: Animator) {
       Log.e("Animation:", "repeat")
   }

  })

顺便说一句,你可以在这里看到原始文档: https://airbnb.io/lottie/#/android