使 Lottie 动画仅在播放时可见

Make Lottie animation visible only while it is playing

为了实现标题中所述的目标,我有两个单独的问题。

第一个问题是:com.airbnb.lottie.LottieAnimationView 在 XML 中默认的可见性状态是什么?我在 XML 中使用了两个不同的 LottieAnimationView,它们具有相同的特性:

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottie_animation_ribbon_and_confetti"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:lottie_autoPlay="false"
        app:lottie_fileName="exploding-ribbon-and-confetti.json"
        app:lottie_loop="true"
        app:lottie_repeatCount="1"/>

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottie_cat_throws_cup"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/puntuacionTotal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/puntosAtrevimiento"
        app:layout_constraintBottom_toTopOf="@id/textoAtrevimiento"
        app:lottie_autoPlay="false"
        app:lottie_fileName="cat_throws_cup.json"
        app:lottie_loop="true"
        app:lottie_repeatCount="1"
        />

虽然第一个仅在我从代码中使用 lottieanimationview.playAnimation() 时可见,但默认情况下第二个在 Activity 启动时立即可见。

我的第二个问题是由于第一个问题描述的问题。为了解决这个问题,我首先将 android:visibility="gone" 添加到那些 activity 开始时立即可见的 LottieAnimationView 中,然后我尝试了几段代码来使动画在播放时可见,完成后回到隐形状态(均未成功):

一次尝试:

lottieCatThrowsCup.setVisibility(View.VISIBLE);
lottieCatThrowsCup.playAnimation();
if(!(lottieCatThrowsCup.isAnimating())) lottieCatThrowsCup.setVisibility(View.GONE);

另一次尝试:

lottieCatThrowsCup.setVisibility(View.VISIBLE);
lottieCatThrowsCup.playAnimation();
if(!lottieCatThrowsCup.isAnimating())lottieCatThrowsCup.cancelAnimation();

第三次尝试:

lottieCatThrowsCup.setVisibility(View.VISIBLE);
lottieCatThrowsCup.playAnimation();
lottieCatThrowsCup.cancelAnimation();

第四次尝试:

lottieCatThrowsCup.setVisibility(View.VISIBLE);
lottieCatThrowsCup.playAnimation();
lottieCatThrowsCup.addAnimatorListener(this);
@Override
    public void onAnimationEnd(Animator animation) {

        animation.setVisibility(View.GONE);


    }

在我看来,最简单的解决方案是在 com.airbnb.lottie.LottieAnimationView 上使用 xml 属性来指示默认情况下它不必可见,除非它被播放,但它不会'似乎不存在...你们如何解决这个问题?提前致谢。

您还可以使用其他 listeners hide/show Lottie view

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

        @Override
        public void onAnimationEnd(Animator animation) {
            Log.e("Animation:","end");
            lottieCatThrowsCup.setVisibility(View.GONE);
        }

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

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

使用这个:

lottieCatThrowsCup.setVisibility(View.VISIBLE);
lottieCatThrowsCup.playAnimation();
lottieCatThrowsCup.addAnimatorListener(this);

@Override
public void onAnimationEnd(Animator animation) {
    lottieCatThrowsCup.setVisibility(View.GONE);
}

Animator.AnimatorListener 添加到您的 LottieAnimationView 并处理其在 onAnimationStart()onAnimationEnd()[ 中的可见性=19=]

lottieAnimationView.addAnimatorListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animator) {
             // Make the LottieAnimationView visible here    
            }


            @Override
            public void onAnimationEnd(Animator animator) {
             // Hide the LottieAnimationView here
            }


            @Override
            public void onAnimationCancel(Animator animator) {

            }


            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

隐藏:

lottieAnimationView.pauseAnimation()
lottieAnimationView.setImageDrawable(null)

用于显示:

lottieAnimationView.setAnimation(R.raw.lottie)
lottieAnimationView.playAnimation()