svenjacobs
10/27/2022, 7:29 AMenum class BackgroundState { Normal, Highlighted }
@Composable
fun Item() {
var backgroundState by remember { mutableStateOf(BackgroundState.Normal) }
val backgroundColor by animateColorAsState(
targetValue = when (backgroundState) {
BackgroundState.Normal -> Color.Transparent
BackgroundState.Highlighted -> Color.Yellow
},
animationSpec = tween(),
)
LaunchedEffect(Unit) {
delay(250)
backgroundState = BackgroundState.Highlighted
delay(250)
backgroundState = BackgroundState.Normal
delay(250)
backgroundState = BackgroundState.Highlighted
delay(250)
backgroundState = BackgroundState.Normal
}
Box(modifier = Modifier.background(backgroundColor)) {
Text("Hello World")
}
}
Thanks for your help!Yves Kalume
10/27/2022, 7:38 AMrepeatable
as the animation specsvenjacobs
10/27/2022, 7:46 AMrepeatable
. Could you please show a code example?Yves Kalume
10/27/2022, 8:04 AMvar startAnimation by remember {
mutableStateOf(false)
}
val bgColor by animateColorAsState(
targetValue = if (startAnimation) Color.Blue else Color.Black,
animationSpec = repeatable(
iterations = 5,
animation = tween(),
repeatMode = RepeatMode.Reverse
)
)
LaunchedEffect(Unit) {
startAnimation = true
}
Yves Kalume
10/27/2022, 8:06 AMinfiniteRepeatable
Filip Wiesner
10/27/2022, 8:56 AMAnimatable
svenjacobs
10/27/2022, 9:00 AMStylianos Gakis
10/27/2022, 9:25 AM@Composable
fun Item() {
val backgroundColor = remember { Animatable(Color.Transparent) }
LaunchedEffect(Unit) {
repeat(2) {
backgroundColor.animateTo(Color.Yellow, tween(1_000))
backgroundColor.animateTo(Color.Transparent, tween(1_000))
}
}
Box(
modifier = Modifier.background(backgroundColor.value),
) {
Text("Hello World")
}
}
One thing I noticed running this, the vector conversion from Transparent
to Yellow
goes through a really dark color in-between, so might have to play with the vector conversion somehow. Not a color expert though, not quite sure how you’d do that 😅Stylianos Gakis
10/27/2022, 9:35 AM@Composable
fun Item() {
val backgroundColor = remember {
Animatable(Color.Yellow.copy(alpha = 0f))
}
SideEffect { println(backgroundColor.value) }
LaunchedEffect(Unit) {
repeat(2) {
backgroundColor.animateTo(Color.Yellow, tween())
backgroundColor.animateTo(Color.Yellow.copy(alpha = 0f), tween())
}
}
Column {
Box(
modifier = Modifier.background(backgroundColor.value),
) {
Text("Hello World")
}
}
}
Zoltan Demant
10/27/2022, 10:05 AMStylianos Gakis
10/27/2022, 10:10 AM@Composable
fun Item() {
val backgroundAlpha = remember { Animatable(0f) }
LaunchedEffect(Unit) {
repeat(2) {
backgroundAlpha.animateTo(1f, tween())
backgroundAlpha.animateTo(0f, tween())
}
}
Column {
Box(
modifier = Modifier.background(Color.Yellow.copy(alpha = backgroundAlpha.value)),
) {
Text("Hello World")
}
}
}
Zoltan Demant
10/27/2022, 10:17 AMStylianos Gakis
10/27/2022, 10:58 AMZoltan Demant
10/27/2022, 11:14 AM