Jetpack撰写底部表单动画



我正在尝试将底部表单角半径相对于分数动画。问题是,它在开始时默认有进度1,在到达终点时仍在拖动。那么我该如何解决这个问题呢?

这里是进度更新:

D/SHEET: progress: 1.0 isExpanded: false
D/SHEET: progress: 1.0 isExpanded: false
D/SHEET: progress: 0.16564026 isExpanded: false
D/SHEET: progress: 0.30916774 isExpanded: false
D/SHEET: progress: 0.44451225 isExpanded: false
D/SHEET: progress: 0.5586605 isExpanded: false
D/SHEET: progress: 0.65918773 isExpanded: false
D/SHEET: progress: 0.7307293 isExpanded: false
D/SHEET: progress: 0.80399936 isExpanded: false
D/SHEET: progress: 0.84945077 isExpanded: false
D/SHEET: progress: 0.8997049 isExpanded: false
D/SHEET: progress: 0.9339056 isExpanded: false
D/SHEET: progress: 0.9522736 isExpanded: false
D/SHEET: progress: 0.9697125 isExpanded: false
D/SHEET: progress: 0.9797893 isExpanded: false
D/SHEET: progress: 0.99829936 isExpanded: false
D/SHEET: progress: 0.9998195 isExpanded: false
D/SHEET: progress: 0.9999819 isExpanded: false
D/SHEET: progress: 1.0 isExpanded: false
D/SHEET: progress: 1.0 isExpanded: true

我试过的代码:

val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
bottomSheetState = 
rememberBottomSheetState(BottomSheetValue.Collapsed)
)
val radius =
if (bottomSheetScaffoldState.bottomSheetState.progress == 1f && 
bottomSheetScaffoldState.bottomSheetState.isCollapsed) 30.dp
else (30 * (1f - 
bottomSheetScaffoldState.bottomSheetState.progress)).dp
BottomSheetScaffold(
scaffoldState = bottomSheetScaffoldState,
sheetContent = {
SheetContent(tracks)
},
sheetShape = RoundedCornerShape(
bottomStart = 0.dp,
bottomEnd = 0.dp,
topStart = radius,
topEnd = radius
),
sheetElevation = 8.dp,
sheetPeekHeight = 400.dp,
modifier = Modifier.fillMaxSize()

) {
MainSheetContent(album)
}

所以,经过许多小时,我找到了这个动画问题的解决方案。我用这个来计算bottomSheetHeight(屏幕高度,你可以通过配置):

val bottomSheetHeight = screenHeight - sheetState.bottomSheetState.requireOffset().roundToInt().dp
val fraction = (bottomSheetHeight.value / screenHeight.value + 0.37f) * 0.73f

注。分数必须在0…1和使用log我确定分数的范围是-0.37..因此,使用一些数学操作,我们得到了想要的范围。

希望它能帮助到别人:)