Archie
05/22/2021, 7:09 PMColor.Red -> Color.Yellow -> Color.Green
. I wanted to be able to get the color given a certain percentage. I tried updateTransition(...)
but it seems that it only applies to a targeted state but not the in-betweens. Any advice? Thanks in advance.Doris Liu
05/22/2021, 7:11 PMArchie
05/22/2021, 7:15 PMlerp
only accepts two value? and if so does that mean stitching two `lerp`s together?Doris Liu
05/22/2021, 7:18 PMColor.Red -> Color.Yellow -> Color.Green
, at what percentage do you expect the color to be yellow? Or is yellow the color for a specific state?Archie
05/22/2021, 7:26 PMColor.Red
at 0f
, Color.Yellow
at 0.5f
and Color.Green
at 1.0f
and if the value is in between ex. 0.25f
it must must be the color between Color.Red and Color.Yellow
probably an orange-y color?Doris Liu
05/22/2021, 7:33 PM0.25f
would in the range [0f, 0.5f], and it's halfway in that range (i.e. 0.25f/(0.5f - 0f) = 50%), then you can use the lerp
with colors that are specific for the [0, 0.5f] range.Archie
05/22/2021, 7:36 PMObjectAnimator.ofArgb(Color.Red, Color.Yellow, Color.Green)
would do the same thing, and was just wondering it something similar was available in compose?Doris Liu
05/22/2021, 7:41 PMObjectAnimator
really brings back memories. 🤣
I was under the impression that you wanted to manually seek the Colors. If the goal is to animate the color through a few way-point colors, consider using keyframes
as your choice of AnimationSpec
Doris Liu
05/22/2021, 7:45 PMkeyframes<Color> {
durationMillis = 100
Color.Red at 0
Color.Yellow at 50
Color.Green at 100
}
See https://developer.android.com/reference/kotlin/androidx/compose/animation/core/KeyframesSpecArchie
05/22/2021, 7:59 PManimationSpec
?Doris Liu
05/22/2021, 8:07 PMdurationMillis
if any. Alternatively, you could skip defining the color values at time 0 and time durationMillis
and have the keyframes derive those from the initial value and target value. e.g.: animateColorAsState(if (foo) Color.Red else Color.Green, animationSpec = keyframes { Color.yellow at durationMillis * 0.5f })
Archie
05/22/2021, 8:11 PMDoris Liu
05/22/2021, 8:11 PMArchie
05/22/2021, 8:23 PMColumn {
var percentage by remember {
mutableStateOf(0.45f)
}
Slider(
value = percentage,
onValueChange = { value ->
percentage = value
},
)
val color = myCustomLerp(
Color.Red,
Color.Yellow,
Color.Green,
percentage
)
...
}
Doris Liu
05/22/2021, 8:45 PMTargetBasedAnimation
using the keyframes, and drive it yourself: https://developer.android.com/jetpack/compose/animation#targetbasedanimationArchie
05/24/2021, 1:54 PMChachako
05/24/2021, 2:29 PMObjectAnimator
. I want to make a scale animation, which are [1, 1.5, -1, 2], `ObjectAnimator`/`ValueAnimator` only needs to provide a duration and it will automatically handle the time corresponding to these values for me, but Compose seems to be unable to... I miss it a bit.Archie
05/24/2021, 3:59 PMfun lerp(
stops: List<Color>,
fraction: Float
): Color {
assert(stops.isNotEmpty())
val segment: Float = 1f / (stops.size - 1)
val startIndex = 0.coerceAtLeast((fraction / segment).toInt())
val endIndex = (startIndex + 1).coerceAtMost(stops.size - 1)
val segmentFraction = (fraction % segment) / segment
return androidx.compose.ui.graphics.lerp(stops[startIndex], stops[endIndex], segmentFraction)
}
The lerp function is for Color
but you could easily replace it with whatever type you need.Doris Liu
05/24/2021, 11:55 PMval anim = TargetBasedAnimation(
initialValue = 1f,
targetValue = 2f,
typeConverter = Float.VectorConverter, animationSpec = keyframes {
durationMillis = 300
1.5f at 100
-1f at 200
})
var percentage : Float = 0.2f
anim.getValueFromNanos((anim.durationNanos * percentage).toLong())
Doris Liu
05/25/2021, 12:00 AMTargetBasedAnimation
. For Transition
level seeking, we are working on a solution - there's a lot implications for seeking a Transition
that we need to work out before we can comfortably put that feature in an API. 🙂Chachako
05/25/2021, 2:45 AManimAsState
? This is great, but I personally think that it’s a bit boilerplate code. Is it possible to solve this problem in the future?Doris Liu
05/25/2021, 2:52 AMDoris Liu
05/25/2021, 2:53 AMTargetBasedAnimation.getValueByProgress(..)
since it seems like it could be useful in other cases as well.Chachako
05/25/2021, 3:47 AM