我想在 compose 中复制 Google 周历视图,但在滚动时卡住了。我使用detectTransformGestures()
来缩放和翻译内容,但是一旦我松开手指,翻译就会停止。你将如何添加滚动行为,一旦你放手就会继续滚动(就像普通的 Recyclerview 或.scrolling()
在 compose 中一样)。
我找到了https://github.com/alamkanak/Android-Week-View但似乎过于复杂并且在 xml 中使用。我想要更干净、更容易使用的东西。
这是我当前的代码。内容还不仅限于顶部和底部,您可以根据需要缩放多少。
@Composable
fun WeekCalendar() {
var scaleY by remember {
mutableStateOf(1f)
}
var offset by remember {
mutableStateOf(0f)
}
var centroi by remember {
mutableStateOf(0f)
}
Canvas(modifier = Modifier
.pointerInput(Unit) {
this.detectTransformGestures() { centroid, pan, zoom, rotation ->
scaleY *= zoom
offset += pan.y * zoom
// center zooming around point location
offset += (zoom - 1) * (offset - centroid.y)
centroi = centroid.y
}
}
.fillMaxWidth()
.height(1200.dp)
) {
val bottom = size.height * scaleY + offset
val top = offset
drawLine(
color = Color.Red,
start = Offset(0f, top),
end = Offset(size.width, top),
)
drawLine(
color = Color.Red,
start = Offset(size.width, top),
end = Offset(size.width, bottom),
)
drawLine(
color = Color.Red,
start = Offset(size.width, bottom),
end = Offset(0f, bottom),
)
drawLine(
color = Color.Red,
start = Offset(0f, bottom),
end = Offset(0f, top),
)
drawLine(
color = Color.Red,
start = Offset(0f, top),
end = Offset(size.width, bottom),
)
drawPoints(listOf(Offset(size.width / 2, centroi)), PointMode.Points, color = Color.Blue, strokeWidth = 5f)
}
}