问题
I have a fragment, here is the onCreateView method:
@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;
}
I need to remove the fragment using getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
when the lottie animation has ended. If I understand correctly, when the isAnimating()
lottie method returns false the animation has ended and since in my configuration the animation does not loop this is when I should remove the current fragment. But I can't just use an if statement since when it is executed the animation may still be going.
I need a way to remove the fragment when the lottie animation ends, how do I do this?
回答1:
This code works for me:
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");
}
});
I hope this solve your problem :)
回答2:
that simple in your 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>
in your 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
}
If you want to count animation frames precisely, use the LottieFiles website

来源:https://stackoverflow.com/questions/46180874/how-to-know-when-lottie-animation-is-completed