[英]Ripple with rounded corners Jetpack Compose
在这个答案中,我得到了错误的波纹 animation。 你知道如何使用 Jetpack Compose 创建带圆角的波纹吗?
使用默认波纹我有这个:
代码:
Card(shape = RoundedCornerShape(30.dp),
border = BorderStroke(width = 2.dp, color = buttonColor(LocalContext.current)),
backgroundColor = backColor(LocalContext.current),
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(radius = 30.dp)
) { show = !show }
) { ... } //Show is animation of other element.
//如果我将波纹半径设置为 200 dp(这是卡片的高度)波纹工作不正常。
从1.0.0-beta08
开始,您可以使用Card
中的onClick
参数而不是clickable
修饰符来解决此问题:
Card(shape = RoundedCornerShape(30.dp),
modifier = Modifier
.fillMaxWidth()
.padding(10.dp),
onClick = { show = !show }
)
直到1.0.0-beta07
对Card
应用.clickable
修饰符之前,涟漪不会被布局的边界剪裁。
作为解决方法,您可以将.clickable
修饰符应用于卡片的内容(例如Box
):
Card(
shape = RoundedCornerShape(30.dp),
border = BorderStroke(width = 2.dp, color = Color.Blue),
backgroundColor = Color.White,
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
) {
Box(Modifier
.clickable(
onClick = { /* ...*/ }
)
){
Text("Text")
}
}
到目前为止,我已经确定了 2 个选项:
.clip
修饰符可以使用相同的形状来剪辑卡片:Card(
shape = RoundedCornerShape(30.dp),
modifier = Modifier
.clip(RoundedCornerShape(30.dp))
.clickable {
//do something
}
) {
Box {
Text("Text")
}
}
这种方法的缺点是高程阴影也会被剪辑,因此您的卡片会丢失它的阴影。
.clickable
: Card(
shape = RoundedCornerShape(30.dp)
) {
Box(
modifier = Modifier.clickable {
//do something
}
) {
Text("Text")
}
}
当您使用长按或其他手势时,您可以使用修饰符.indication
val interactionSource = remember { MutableInteractionSource() }
Card(
modifier = Modifier
.padding(12.dp, 6.dp)
.fillMaxWidth()
.clip(RoundedCornerShape(12.dp))
.indication(interactionSource, LocalIndication.current)
.pointerInput(Unit) {
detectTapGestures(
onPress = { offset ->
val press = PressInteraction.Press(offset)
interactionSource.emit(press)
tryAwaitRelease()
interactionSource.emit(PressInteraction.Release(press))
},
onLongPress = {},
onTap = {}
)
}
)
希望这将为您提供最简单的解决方案
只需在修改器参数中添加.clip(RoundedCornerShape(30.dp))
简短的细节:
这是完整的代码:
Card(modifier = Modifier
.padding(30.dp)
.size(100.dp)
.clip(RoundedCornerShape(30.dp))
.clickable {
// After click //
},
shape = RoundedCornerShape(30.dp),
elevation = 4.dp
) { }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.