android - Jetpack Compose Surface 点击波纹没有根据形状剪裁?

标签 android android-jetpack-compose

我有 3 个表面,如 gif 中所示,当我单击涟漪效应传播时没有考虑表面的形状。
enter image description here
哪些是用

@Composable
fun SurfaceClickPropagationExample() {

    // Provides a Context that can be used by Android applications
    val context = AmbientContext.current

    // 🔥 Offset moves a component in x and y axes which can be either positive or negative
    // 🔥🔥 When a component inside surface is offset from original position it gets clipped.
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .clipToBounds()
            .clickable(onClick = {})
    ) {

        Surface(
            modifier = Modifier
                .preferredSize(150.dp)
                .padding(12.dp)
                .clickable(onClick = {
                })
                .clipToBounds(),
            elevation = 10.dp,
            shape = RoundedCornerShape(10.dp),
            color = (Color(0xFFFDD835))
        ) {

            Surface(
                modifier = Modifier
                    .preferredSize(80.dp)
                    .clipToBounds()
                    .offset(x = 50.dp, y = (-20).dp)
                    .clickable(onClick = {
                    }),
                elevation = 12.dp,
                shape = CircleShape,
                color = (Color(0xFF26C6DA))
            ) {

            }
        }

        Surface(
            modifier = Modifier
                .preferredSize(110.dp)
                .padding(12.dp)
                .offset(x = 110.dp, y = 20.dp)
                .clickable(onClick = {}),
            shape = CutCornerShape(10.dp),
            color = (Color(0xFFF4511E)),
            elevation = 8.dp
        ) {}
    }
}
我添加了 Modifier.clipToBounds()检查它是否适用于它,但无论有没有它都不起作用。

最佳答案

compose 版本 1.0.0-beta08 更新:
使用接受 onClick 的 Surface 的新实验性重载。

@ExperimentalMaterialApi
@Composable
fun Surface(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    shape: Shape = RectangleShape,
    color: Color = MaterialTheme.colors.surface,
    contentColor: Color = contentColorFor(color),
    border: BorderStroke? = null,
    elevation: Dp = 0.dp,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    indication: Indication? = LocalIndication.current,
    enabled: Boolean = true,
    onClickLabel: String? = null,
    role: Role? = null,
    content: () -> Unit
): @ExperimentalMaterialApi @Composable Unit
文档:https://developer.android.com/reference/kotlin/androidx/compose/material/package-summary#Surface(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.foundation.BorderStroke,androidx.compose.ui.unit.Dp,androidx.compose.foundation.interaction.MutableInteractionSource,androidx.compose.foundation.Indication,kotlin.Boolean,kotlin.String,androidx.compose.ui.semantics.Role,kotlin.Function0)

尝试申请 Modifier.clip(shape: Shape) 之前 Modifier.clickable .
在撰写中使用修饰符时,顺序很重要。首先出现的修饰符元素将首先应用。 (documentation)

关于android - Jetpack Compose Surface 点击波纹没有根据形状剪裁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65548150/

相关文章:

android - 如何在jetpack compose中为剪切区域添加圆角圆弧?

android-jetpack-compose - 如何从子可组合项控制脚手架的 FloatingActionButton onClick?

android - 自 Compose BOM 2023.08.00 以来,仪器测试失败,因为惰性列表更改似乎在层次结构中留下了旧节点

android - 防止 WebView 打开浏览器

android - 如何对 android.graphics.Bitmap 和 BitmapFactory 进行单元测试?

android - 选择正确的聊天协议(protocol)实现

android - 我如何使用 'glGenVertexArrays' 使用 android-ndk