https://kotlinlang.org logo
#compose
Title
# compose
k

Kazemihabib1996

04/12/2020, 8:07 PM
I've been trying this animationDemo https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/ui/ui-animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SingleValueAnimationDemo.kt but I see no animation in that, the color directly changes from
Color(0.0, 1.0, 0.0, 1.0, sRGB IEC61966-2.1)
to
Color(1.0, 0.0, 0.0, 1.0, sRGB IEC61966-2.1)
Copy code
@Composable
fun SingleValueAnimationDemo() {
    val enabled = state { true }
    Clickable({ enabled.value = !enabled.value }) {
        val color = animate(if (enabled.value) Color.Green else Color.Red)
        Box(Modifier.fillMaxSize(), backgroundColor = color)
    }
}
I can create this animation this way:
@Composable
fun SingleValueAnimationDemo() {
val enabled = state { true }
Clickable({ enabled.value = !enabled.value }) {
val animColor = animatedColor(if (enabled.value) Color.Green else Color.Red)
animColor.animateTo(if (enabled.value) Color.Red else Color.Green, anim = TweenBuilder<Color>().apply {
duration = 1000
easing = LinearEasing}
)
Log.d("Animation", animColor.value.toString())
Box(Modifier.fillMaxSize(), backgroundColor = animColor.value)
}
}
but I don't understand why the above not works
I found the problem https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/ui/ui-animation/src/main/java/androidx/ui/animation/SingleValueAnimation.kt#121
Copy code
val anim = remember(clock, converter) {
    AnimatedValueModel(target, converter, clock)
}
converter changes each time so the callback reevaluates then we have new
anim
then it just do
return anim.value
that's are new color
I don't see any reason to pass converter to remember here 🤔
v

Vinay Gaba

04/13/2020, 2:38 AM
I had created a modified example of this where the colors continuously change and they keep looping. I ended up using
state
as well but I’m sure there’s a better way to do this - https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/animation/Animation2Activity.kt
this is the result of the example
k

Kazemihabib1996

04/13/2020, 7:23 AM
thank you @Vinay Gaba I also made it work by modifying the source of animate function seems that there is a bug in animate function that I explained above. I just wanted to get some one elses approvement of this bug before filling a bug report.