Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animation doesn't load when doing LottieDrawable#setComposition() in fromAssetFileName() #458

Closed
drinkthestars opened this issue Sep 8, 2017 · 3 comments

Comments

@drinkthestars
Copy link

Hey! :)

When I try to do something like:

        String animationFileName = "something.json"
        
        LottieDrawable lottieDrawable = new LottieDrawable();
        
        lottieAnimationView.setImageDrawable(lottieDrawable);
        
        LottieComposition.fromAssetFileName(context, animationFileName, (composition) -> {
            lottieDrawable.setComposition(composition);
            lottieDrawable.loop(true);
            lottieDrawable.playAnimation();
        });

The animation doesn't show up

Using 2.2.0 (since I'm still on compile/target/support lib version 25.0.1) , not sure if its fixed on 2.2.1

Related to #37

@gpeal
Copy link
Collaborator

gpeal commented Sep 10, 2017

@drinkthestars Your extra code is unnecessary. Just call lottieAnimationView.setAnimation("something.json")
If that doesn't fix it, can you attach a sample project that reproduces it?

@drinkthestars
Copy link
Author

Thanks for the quick reply!

Yes, I tried to use setAnimation(...). So, if I do:

// trying to load and play first animation
lottieView.setAnimation("animation1.json");
lottieView.playAnimation();

// trying to load and play second animation
lottieView.setAnimation("animation2.json");
lottieView.playAnimation();

I see playAnimation() seems to play the animation that the LottieAnimationView's LottieDrawable currently holds.

If after calling lottieView.setAnimation("animation2.json"), animation2.json isn't fully loaded yet, the second time I call playAnimation(), I see "animation1.json" playing, and a jarring effect as "animation2.json" continues to load up.

From my understanding, this seems to happen because LottieAnimationView#setAnimation(...) is internally loading up the LottieComposition asynchronously (as I see here).
So there's no way to hook into when setAnimation(...) actually completes and finishes loading the animation in its entirety.

This is why I found a need to load the LottieComposition + set + play the animation manually. Is there another way to guarantee that the animation is fully loaded up before calling playAnimation() ?

(l will try to attach a sample project shortly)

@drinkthestars
Copy link
Author

drinkthestars commented Sep 28, 2017

Update~~

Realized what was going wrong. After setting the LottieDrawable on the LottieAnimationView:

        LottieDrawable lottieDrawable = new LottieDrawable()
        lottieAnimationView.setImageDrawable(lottieDrawable)

If I called playAnimation() on the view like so:

        lottieAnimationView.playAnimation()

The animation wouldn't play. Then I tried calling playAnimation() on the LottieDrawable and that seemed to do the trick! Looks like this is because LottieAnimationView keeps its own LottieDrawable and that reference is only local, and calling lottieAnimationView.setImageDrawable(...) won't replace the view's underlying LottieDrawable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants