Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Lottie animation visible only while it is playing

Tags:

android

lottie

I have two separate questions in order to achieve the goal stated in the title.

The first question is: what's the visibility state of a com.airbnb.lottie.LottieAnimationView by default in the XML? I have used two different LottieAnimationView's in my XML with the same characteristics:

    <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"
        />

And while the first is only visible when I use lottieanimationview.playAnimation() from code, the second is immediately visible by default when the Activity starts.

My second question is due to the problem described in the first question. To solve this problem I first added android:visibility="gone" to those LottieAnimationViewthat were immediately visible when the activity started, and then I tried several pieces of code to make the animations visible when they were played and back to invisible after finished (all without success):

One attempt:

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

Another attempt:

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

Third attempt:

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

Fourth attempt:

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

        animation.setVisibility(View.GONE);


    }

To my mind, the easiest solution would be to use an xml attribute on com.airbnb.lottie.LottieAnimationViewto indicate that it doesn't have to be visible by default unless it's played, but it doesn't seem to exist one... How would you guys solve this? Thanks in advance.

like image 921
assensi Avatar asked Aug 07 '19 09:08

assensi


People also ask

How do you play Lottie animation only once?

The Lottie animation will play infinite times, so to make sure that it only plays once, we are going to use the useRef and useEffect hooks so that it only plays once for 3 secs (3000 ms) when the app loads for the first time. We are also going to use the Animated module from React Native.

How do I Preview Lottie?

Start LottieCloud, hit the browse button or just drag your json files into the browser. Get your LottieCloud ID by clicking the user icon on the top right. Open the LottieCloudPlayer app and scan or enter your ID. That's it.

Are Lottie animations accessible?

LottieFiles Animations are accessible across 25,000+ everyday tools with the embed feature. Lottie's latest tech upgrade, the EMBED feature, will now enable designers, creators & businesses to multi-layer their work with exciting animations.

How do I create a Lottie animation in Java?

In the activity_main.xml file declare a Lottie object by specifying the parameters. Also declare two Buttons, one to start the animation and one to pause it. The primary parameters are:

Does Lottie play/loop the animation?

I found that almost any Lottie animation displays just as a still image and does not play/loop the animation. I followed the guide on LottieFiles website and also information that I found on other questions but I still did not manage to get the animation playing. The following is exactly what I have added:

How do I animate the scroll of the Lottie player?

The animation seeks frame by frame as you scroll down the website. Copied! To wrap the Lottie player inside a container or sync the Lottie scroll with a div element on your page, pass a container variable with the container ID to the action object. In the following example, the container ID is MyContainerId.

How do I place Lottie on my website?

You can place the Lottie animation anywhere on your website. The animation seeks frame by frame as you scroll down the website. Copied! To wrap the Lottie player inside a container or sync the Lottie scroll with a div element on your page, pass a container variable with the container ID to the action object.


2 Answers

Add Animator.AnimatorListener to your LottieAnimationView and handle its visibility in onAnimationStart() and onAnimationEnd()

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

            }
        });
like image 29
Rishabh Sagar Avatar answered Oct 10 '22 14:10

Rishabh Sagar


There are other listeners as well that you can use to 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");
        }
    });
like image 173
Prithvi Bhola Avatar answered Oct 10 '22 15:10

Prithvi Bhola