Comments (6)
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.
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.
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.
What are your "blocking actions"? That doesn't sound like something that should be happening in the body of a composable.
from lottie-android.
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.
Discovered further, slighter bug.
from lottie-android.
Related Issues (20)
- Migrating Compose-BOM library to alpha version.
- Lottie-compose feature parity HOT 2
- Uncaught exception on Lottie that causes large canvas allocation HOT 1
- Binary incompatibility between 6.3.0 and 6.4.0 HOT 2
- java.lang.IllegalStateException: Missing values for keyframe.
- LottieAnimationView.duration returns 0 HOT 3
- Incorrect icon loading when manually selecting a dark or light theme HOT 1
- NullPointer crash problem
- [6.3.0] use compatible lottie resource but crash HOT 4
- Provide a Global Configuration to Default Cache Composition to Disabled HOT 1
- Improve Gzip and Zip animation detection for `File` and `ContentProvider` animations in Compose HOT 2
- 6.4.0 Delete the setScale API. Are there any other alternative APIs that do the same thing as the setScale API? HOT 1
- Correct the frequent GC issues caused by L.beginSection and L.endSection methods. HOT 1
- Cannot render when using LottieDrawable without setting bounds with SOFTWARE rendering HOT 4
- Execution failed for task ':app:mergeExtDexDebug' HOT 1
- Option to exclude network cache from `LottieCompositionFactory.clearCache()`
- AnimatorListener触发错误
- safeMode is not catching RecordingCanvas.throwIfCannotDraw
- Thread pool size will never go beyond corePoolSize if an unbounded queue is used.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lottie-android.