我想从我的初始位置垂直滑出到另一个点非常慢。假设我的初始位置是50.dp
,那么它会到49.dp
,48.dp
...... 0.dp
。我尝试了一些代码,但直接到0.dp,然后隐藏我不想。你们能帮助我吗?
@OptIn(ExperimentalAnimationApi::class)
@Preview(showBackground = true)
@Composable
fun MoveText() {
var visible by remember { mutableStateOf(true) }
var yPosition by remember {
mutableStateOf(50.dp)
}
LaunchedEffect(key1 = visible) {
yPosition = if (visible) {
50.dp
} else {
0.dp
}
}
ScrollComposeTheme {
Column(
modifier = Modifier
.fillMaxSize()
.padding(start = 16.dp, top = 16.dp)
) {
AnimatedVisibility(
visible = visible,
modifier = Modifier.padding(top = yPosition),
exit = slideOutVertically(
animationSpec = tween(4000),
)
) {
val color by transition.animateColor(label = "color") { state ->
if (state == EnterExitState.Visible) Color.Black else Color.Gray
}
Column {
Icon(
imageVector = Icons.Default.ShoppingCart,
tint = color,
contentDescription = null,
)
Text(
text = "Hello, Anna",
fontSize = 20.sp,
color = color,
)
}
}
Button(
modifier = Modifier
.absoluteOffset(y = 10.dp),
onClick = {
visible = !visible
},
) {
Text(text = "Move Text")
}
}
}
}
1条答案
按热度按时间8xiog9wr1#
经过研究,我得到了使用
animateDpAsState
,这是修复我的问题。我在这里添加一个代码