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

时间:2017-09-12 15:56:07

标签: android android-fragments lottie-android

我有一个片段,这里是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 animation is finished.


    return mView;
}

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

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

3 个答案:

答案 0 :(得分:17)

此代码适用于我:

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");
        }
    });

我希望这可以解决你的问题:)

答案 1 :(得分:4)

科特琳动画代码完成:

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?) {
            }

        })

答案 2 :(得分:-1)

在my_layout.xml中简单

 <RelativeLayout
   android:id="@+id/contanierForAnimationId"
   android:layout_width="50dp"
   android:layout_height="50dp"
   android:visibility="visible">
      <com.airbnb.lottie.LottieAnimationView
          android:id="@+id/aLottieAnimationId"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:lottie_autoPlay="false"
          app:lottie_fileName="animation.json"
          app:lottie_repeatCount="0" />
</RelativeLayout>

在您的my_class.kt

var aLottieAnimationId : LottieAnimationView? = null
var contanierForAnimationId: RelativeLayout? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, 
savedInstanceState: Bundle?): View? {

    val view = inflater.inflate( R.layout.my_Layout, container, false )
    aLottieAnimationId = view.findViewById(R.id.aLottieAnimationId) as LottieAnimationView
contanierForAnimationId = view.findViewById(R.id.contanierForAnimationId) as RelativeLayout


    aLottieAnimationId?.setOnClickListener {
      aLottieAnimationId?.setOnClickListener(null)
      aLottieAnimationId?.speed = 1f
      aLottieAnimationId?.playAnimation()
      aLottieAnimationId?.setMinAndMaxFrame(0, 90) //How many frames does the animation have
       aLottieAnimationId?.addAnimatorUpdateListener { valueAnimator ->
         // Set animation progress
           val progress = (valueAnimator.animatedValue as Float * 100).toInt()
           Log.d("progress :","$progress")
         //You must know How many frames does the animation have
           if (progress == 90) {
             contanierForAnimationId?.visibility = View.GONE
           }
       }
    } 


  return view
}

如果要精确计算动画帧,请使用LottieFiles网站 lottiefiles example