![](/img/trans.png)
[英]Convert Pixel to Dp for Custom Rounded Indication Clickable in Jetpack Compose
[英]What is clickable indication in jetpack compose?
指示 按下修改的元素时显示的指示。 默认情况下,将使用来自 [LocalIndication] 的指示。 通过
null
不显示任何指示,或通过 [LocalIndication] 的当前值显示主题默认值
当您单击一个项目时,默认情况下会显示涟漪效应
您可以从您的主题中获取它,使用 rememberRipple 或者您可以编写您自己的指示
本教程中提供了以下示例以及更多适用于 Compose的示例。 你可以测试一下
使用rememberRipple()
@Composable
fun CustomRippleExample() {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
Box(
contentAlignment = Alignment.Center,
modifier = modifierWithClip
.clickable(
interactionSource = MutableInteractionSource(),
indication = rememberRipple(
bounded = true,
radius = 250.dp,
color = Color.Green
),
onClick = {}
)
) {
Text(
text = "rememberRipple() bounded",
color = Color.White
)
}
Spacer(modifier = Modifier.height(8.dp))
Box(
contentAlignment = Alignment.Center,
// 🔥 Modifier.clip also bounds ripple
modifier = modifierNoClip
.clickable(
interactionSource = MutableInteractionSource(),
indication = rememberRipple(
bounded = false,
radius = 250.dp,
color = Color.Green
),
onClick = {}
)
) {
Text(
text = "rememberRipple() unbounded",
color = Color.White
)
}
}
}
从你的主题
@Composable
fun CustomRippleThemeExample() {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
CompositionLocalProvider(LocalRippleTheme provides CustomRippleTheme(Color.Cyan)) {
Box(
modifier = modifierWithClip.clickable {},
contentAlignment = Alignment.Center
) {
Text(
text = "Custom Ripple Theme",
color = Color.White
)
}
}
Spacer(modifier = Modifier.height(8.dp))
CompositionLocalProvider(LocalRippleTheme provides CustomRippleTheme(Color.Magenta)) {
Box(
modifier = modifierWithClip.clickable {},
contentAlignment = Alignment.Center
) {
Text(
text = "Custom Ripple Theme",
color = Color.White
)
}
}
}
}
自定义指示
private class CustomIndication(
val pressColor: Color = Color.Red,
val cornerRadius: CornerRadius = CornerRadius(16f, 16f),
val alpha: Float = 0.5f,
val drawRoundedShape: Boolean = true
) : Indication {
private inner class DefaultIndicationInstance(
private val isPressed: State<Boolean>,
) : IndicationInstance {
override fun ContentDrawScope.drawIndication() {
drawContent()
when {
isPressed.value -> {
if (drawRoundedShape) {
drawRoundRect(
cornerRadius = cornerRadius,
color = pressColor.copy(
alpha = alpha
), size = size
)
} else {
drawCircle(
radius = size.width,
color = pressColor.copy(
alpha = alpha
)
)
}
}
}
}
}
@Composable
override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
val isPressed = interactionSource.collectIsPressedAsState()
return remember(interactionSource) {
DefaultIndicationInstance(isPressed)
}
}
}
例子
@Composable
fun CustomIndicationExample() {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
val indication1: CustomIndication = CustomIndication(
pressColor = Color.Cyan,
cornerRadius = CornerRadius(30f, 30f),
alpha = .7f
)
val indication2: CustomIndication = CustomIndication(
pressColor = Color.Red,
cornerRadius = CornerRadius(16f, 16f),
alpha = .5f
)
val indication3: CustomIndication = CustomIndication(
pressColor = Color(0xffFFEB3B),
alpha = .4f,
drawRoundedShape = false,
)
Box(
modifierWithClip
.clickable(
interactionSource = MutableInteractionSource(),
indication = indication1,
onClick = {}
),
contentAlignment = Alignment.Center) {
Text(
text = "Custom Indication",
color = Color.White
)
}
Spacer(modifier = Modifier.height(8.dp))
Box(
modifierWithClip
.clickable(
interactionSource = MutableInteractionSource(),
indication = indication2,
onClick = {}
),
contentAlignment = Alignment.Center) {
Text(
text = "Custom Indication",
color = Color.White
)
}
Spacer(modifier = Modifier.height(8.dp))
Box(
Modifier.fillMaxWidth()
.height(200.dp)
.clickable(
interactionSource = MutableInteractionSource(),
indication = indication3,
onClick = {}
),
contentAlignment = Alignment.Center) {
Text(
text = "Custom Indication with Circle Shape",
color = Color.White
)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.