在我的Android项目中,我正在做一个简单的浮动操作按钮,它可以展开并显示执行不同操作的按钮列表. 为了跟踪FAB的当前状态,我有下一个枚举类
enum class FabState {
Expanded,
Collapsed
}
为了显示浮动操作按钮,我有以下可组合函数:
@Composable
fun MultiFloatingActionButton(
icon: ImageVector,
iconTint: Color = Color.White,
miniFabItems: List<MinFabItem>,
fabState: FabState, //The initial state of the FAB
onFabStateChanged: (FabState) -> Unit,
onItemClick: (MinFabItem) -> Unit
) {
val transition = updateTransition(targetState = fabState, label = "transition")
val rotate by transition.animateFloat(label = "rotate") {
when (it) {
FabState.Collapsed -> 0f
FabState.Expanded -> 315f
}
}
val fabScale by transition.animateFloat(label = "fabScale") {
when (it) {
FabState.Collapsed -> 0f
FabState.Expanded -> 1f
}
}
val alpha by transition.animateFloat(label = "alpha") {
when (it) {
FabState.Collapsed -> 0f
FabState.Expanded -> 1f
}
}
val shadow by transition.animateDp(label = "shadow", transitionSpec = { tween(50) }) { state ->
when (state) {
FabState.Expanded -> 2.dp
FabState.Collapsed -> 0.dp
}
}
Column(
horizontalAlignment = Alignment.End
) { // This is where I have my question, in the if condition
if (fabState == FabState.Expanded || transition.currentState == FabState.Expanded) {
miniFabItems.forEach { minFabItem ->
MinFab( //Composable for creating sub action buttons
fabItem = minFabItem,
alpha = alpha,
textShadow = shadow,
fabScale = fabScale,
onMinFabItemClick = {
onItemClick(minFabItem)
}
)
Spacer(modifier = Modifier.size(16.dp))
}
}
FloatingActionButton(
onClick = {
onFabStateChanged(
when (fabState) {
FabState.Expanded -> {
FabState.Collapsed
}
FabState.Collapsed -> {
FabState.Expanded
}
}
)
}
) {
Icon(
imageVector = icon,
tint = iconTint,
contentDescription = null,
modifier = Modifier.rotate(rotate)
)
}
}
}
我定义的常量用于根据FAB状态显示/隐藏按钮的动画.
当我第一次做这个函数时,最初的条件给了我一个不同的行为,并try 了所有可能的条件,我得到了3个不同的结果:
- 第一个条件:
if (transition.currentState == FabState.Expanded) {...}
个
结果:animation not loading from collapsed to expanded, but it does from expanded to collapsed人
- 第二种情况:
if (fabState == FabState.Expanded) {...}
结果:animation loading from collapsed to expanded, but not from expanded to collapsed人
第三个条件(我现在正在使用的条件):
if (fabState == FabState.Expanded || transition.currentState == FabState.Expanded) {...}
个
结果:animation loading in both ways人
所以我的问题是:每一种情况是如何改变动画的行为的?
任何帮助都将不胜感激.提前谢谢你