AnimatedVisibility
中的EnterTransition
和 ExitTransition
,用来配置入场/出场
时候的动画效果。
默认的入场效果是 fadeIn() + expandVertically()
默认的出场效果是 fadeOut() + shrinkVertically()
EnterTransition
和ExitTransition
支持的动画enter
的参数类型是EnterTransition
,支持这些动画
fade: fadeIn
scale: scaleIn
slide: slideIn, slideInHorizontally, slideInVertically
expand: expandIn, expandHorizontally, expandVertically
exit
的参数类型是EnterTransition
,支持这些动画
fade: fadeOut
scale: scaleOut
slide: slideOut, slideOutHorizontally, slideOutVertically
shrink: shrinkOut, shrinkHorizontally, shrinkVertically
可以看到EnterTransition
和ExitTransition
支持的动画只有expand
和shrink
命名上有区别,
其他都是对应的 fadeIn
和 fadeOut
,scaleIn
和 scaleOut
,slideIn
和 slideOut
。
expand
和shrink
命名上做区分,是因为expand
就是展开
的意思,而shrink
是收缩
的意思,它们其实就是相对应的。
fadeIn / fadeOut
是淡出淡出的效果
我们使用上篇文章中的那段代码
@Composable
fun AnimatedVisibilityPage() {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
var visible by remember {
mutableStateOf(true)
}
AnimatedVisibility(visible = visible) {
MyImage()
}
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = { visible = !visible }) {
Text(text = "显示/隐藏")
}
}
}
@Composable
private fun MyImage() {
Image(
painter = painterResource(id = R.mipmap.photot1),
modifier = Modifier.width(300.dp),
contentDescription = null
)
}
然后修改AnimatedVisibility
,配置enter
和exit
AnimatedVisibility(
visible = visible,
enter = fadeIn(),
exit = fadeOut()
) {
MyImage()
}
scaleIn / scaleOut
是缩放的效果
AnimatedVisibility(
visible = visible,
enter = scaleIn(),
exit = scaleOut()
) {
MyImage()
}
slideIn / SlideOut
是滑动的效果,这里进入的初始位置和退出的目标位置都设置为了(300,-150)
,所以会从右上角进入/退出
AnimatedVisibility(
visible = visible,
enter = slideIn(initialOffset = {
IntOffset(300, -150) }
),
exit = slideOut(targetOffset = {
IntOffset(300,-150) }
)
) {
MyImage()
}
slideInVertically / slideOutVertically
是垂直方向滑动进入/退出
AnimatedVisibility(
visible = visible,
enter = slideInVertically(),
exit = slideOutVertically()
) {
MyImage()
}
slideInHorizontally / slideOutHorizontally
是从横向方向滑动进入/退出
AnimatedVisibility(
visible = visible,
enter = slideInHorizontally(),
exit = slideOutHorizontally()
) {
MyImage()
}
expandIn / shrinkOut
是展开/收缩
的效果
AnimatedVisibility(
visible = visible,
enter = expandIn(),
exit = shrinkOut()
) {
MyImage()
}
expandVertically / shrinkVertically
是从垂直方向展开/收缩
AnimatedVisibility(
visible = visible,
enter = expandVertically(),
exit = shrinkVertically()
) {
MyImage()
}
expandHorizontally / shrinkHorizontally
是从横向方向展开/收缩
AnimatedVisibility(
visible = visible,
enter = expandHorizontally(),
exit = shrinkHorizontally()
) {
MyImage()
}
Compose 动画系列,后续持续更新,可以先关注
Compose 动画 (一) : animateXxxAsState 实现放大/缩小/渐变等效果
Compose 动画 (二) : 为什么animateDpAsState要用val ? MutableState和State有什么区别 ?
Compose 动画 (三) : AnimatedVisibility 从入门到深入
关于EnterTransition 和 ExitTransition 示例也可以看这里