![](/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.