GithubHelp home page GithubHelp logo

Comments (6)

gpeal avatar gpeal commented on May 18, 2024 1

Your first example renders LottieAnimation twice which is probably not expected.

In your second example, you have nested composable which you never want to do. Each composable is an anonymous function that will be different each time your outer composable recomposes.

Also, in the second example, you're going to flip dir every time the outer composable renders when progress == 1f which will cause it to flip back and forth as each time you flip the state, it will recompose.

I would recommend something more like this. I think you'll find a lot more flexibility for use cases like this with LottieAnimatable

val composition1 by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
val composition2 by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.wave))
val state = remember { LottieAnimatable() }

LaunchedEffect(composition1, composition2) {
    if (composition1 == null || composition2 == null) return@LaunchedEffect
    state.animate(composition1)
    state.animate(composition2)
}

LottieAnimation(state.composition, { state.progress })

from lottie-android.

gpeal avatar gpeal commented on May 18, 2024 1

I don't think there is an issue with Lottie here. If you'd like support with your specific compose code and how to structure your code, StackOverflow may be a better fit.

But I suspect you lightly want to trigger whatever state/external update you want from the LaunchedEffect at the appropriate time not in the body of your composable.

from lottie-android.

sheinin avatar sheinin commented on May 18, 2024

I rushed to mark it as complete, but there is still a glitch. Once the "state == 1" blocking actions are complete the animation jumps ahead from the first frame of the next composition, so the sequence is not smooth. In Android View, the next iteration of animation is initiated in code after the completion of the blocking action. With @gpeal solution compositions continue while the main thread is blocked resulting in jumpy animation.

val composition1 by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
val composition2 by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.wave))
val state = remember { LottieAnimatable() }

LaunchedEffect(composition1, composition2) {
    if (composition1 == null || composition2 == null) return@LaunchedEffect
    state.animate(composition1)
    state.animate(composition2)
}

LottieAnimation(state.composition, { state.progress })


/* ## THIS LINE ## */
if (state.progress) // perform blocking actions

from lottie-android.

gpeal avatar gpeal commented on May 18, 2024

What are your "blocking actions"? That doesn't sound like something that should be happening in the body of a composable.

from lottie-android.

sheinin avatar sheinin commented on May 18, 2024

The command increases value of an observable in ViewModel, which triggers database queries:

// Composable
if (state.progress == 1f) store.step.postValue(store.step.value?.plus(1))

...

// ViewModel
store.step.observe(this) { count ->
         when (count) {
                 0 -> // query one
                1 -> // query two
                etc...
          }
}

Here's how it works in View:

// Listen to end of Lottie composition
view.findViewById<LottieAnimationView>(R.id.animate).also { an ->
                an.addAnimatorListener(
                    object : Animator.AnimatorListener {
                        override fun onAnimationStart(p0: Animator) {}
                        override fun onAnimationEnd(p0: Animator) {
                                when (step.value) {
                                    0 -> {
                                          //  query one
                                          step.postValue(step.value?.plus(1))
                                    }
                                    1 -> {
                                          //  query two
                                          step.postValue(step.value?.plus(1))
                                    }
                                   ...etc.
..............

// Observable:
private val step = MutableLiveData(-1).also {
        it.observe(this@MainActivity) { i ->
            if (i > 0) {
                val task = LottieCompositionFactory.fromRawRes(this, if (dir) R.raw.anim1 else R.raw.anim2)
                task.addListener { composition ->
                    view.findViewById<LottieAnimationView>(R.id.animate).also { an ->
                        an.setComposition(composition)
                        an.playAnimation()
                    }
                }
                dir = !dir
            }
        }
    }

// 


from lottie-android.

sheinin avatar sheinin commented on May 18, 2024

Discovered further, slighter bug.

from lottie-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.