This animates an unveiling scrim over the content as it enters.
AnimatedContentVeil
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun AnimatedContentVeil() {
var visible by remember { mutableStateOf(true) }
Column {
Button(onClick = { visible = !visible }) { Text("Toggle") }
AnimatedContent(
targetState = visible,
transitionSpec = {
if (targetState) {
(slideInHorizontally { it } togetherWith veilOut()).apply {
targetContentZIndex = 1f
}
} else {
unveilIn() togetherWith slideOutHorizontally { it }
}
},
) { isVisible ->
if (isVisible) {
Text(modifier = Modifier.fillMaxSize().background(Color.Red), text = "Page 2")
} else {
Text(modifier = Modifier.fillMaxSize(), text = "Page 1")
}
}
}
}
AnimatedVisibilityVeil
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun AnimatedVisibilityVeil() {
var visible by remember { mutableStateOf(true) }
Column(modifier = Modifier.background(Color.White)) {
Button(onClick = { visible = !visible }) { Text("Toggle") }
AnimatedVisibility(
visible = visible,
enter = unveilIn(initialColor = Color.White),
exit = veilOut(targetColor = Color.White),
) {
Box(Modifier.fillMaxSize().padding(30.dp).background(Color.Blue)) { Text("Content") }
}
}
}