gpt4 book ai didi

android - Jetpack Compose 中的长按手势

转载 作者:行者123 更新时间:2023-12-04 23:53:01 26 4
gpt4 key购买 nike

如何在长按时使用 jetpack compose 获得此内容大小减小动画,然后在释放时恢复正常(如 Spotify Android 应用程序中的卡)。
这是一个显示动画的 gif。
Spotify Card

最佳答案

您可以使用 Transition 管理按下和释放状态之间的动画。

enum class ComponentState { Pressed, Released }

var toState by remember { mutableStateOf(ComponentState.Released) }
val transition: Transition<ComponentState> = updateTransition(targetState = toState, label = "")

// Defines a float animation to scale x,y
val scalex: Float by transition.animateFloat(
transitionSpec = { spring(stiffness = 50f) }, label = ""
) { state ->
if (state == ComponentState.Pressed) 0.90f else 1f
}
val scaley: Float by transition.animateFloat(
transitionSpec = { spring(stiffness = 50f) }, label = ""
) { state ->
if (state == ComponentState.Pressed) 0.90f else 1f
}
然后你可以使用 PointerInputScope.detectTapGestures 检测按下手势:
val modifier = Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = {
toState = ComponentState.Pressed
tryAwaitRelease()
toState = ComponentState.Released
}
)
}
最后将动画应用到您的 Composable。
例如:
Box(
modifier
.width((100 * scalex).dp)
.height((100 * scaley).dp),
contentAlignment = Alignment.Center) {

Image(
//...
modifier = Modifier.graphicsLayer{
scaleX = scalex;
scaleY = scaley
})
}
enter image description here

关于android - Jetpack Compose 中的长按手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67752131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com