简体   繁体   English

LazyColumn/LazyRow 中的阴影剪切

[英]Shadow clipping in LazyColumn/LazyRow

The shadow is clipping in a very odd way when it's overlapping other items in a LazyRow and I can't figure out why.当阴影与LazyRow中的其他项目重叠时,它以一种非常奇怪的方式剪裁,我不明白为什么。 I'm running this code on TV emulator but I can't imagine that would make any difference.我在电视模拟器上运行这段代码,但我无法想象这会有什么不同。

Attempt 1: Modifier.shadow()尝试 1:Modifier.shadow()

val colors = listOf(
    Color.Red,
    Color.Blue,
    Color.Green,
    Color.Yellow
)

@Composable
fun ListTest() {
    LazyColumn {
        items(30) {
            Column {
                Text("This is row $it")
                LazyRow {
                    items(colors) {
                        var isFocused by remember { mutableStateOf(false) }
                        val alpha = if (isFocused) 1f else 0.25f
                        val elevation = if (isFocused) 40.dp else 0.dp
                        Surface(
                            shape = RoundedCornerShape(8.dp),
                            color = it.copy(alpha = alpha),
                            modifier = Modifier
                                .width(240.dp)
                                .height(150.dp)
                                .padding(start = 16.dp)
                                // 🔴 Look here
                                .shadow(elevation)
                                .onFocusChanged { state ->
                                    isFocused = state.isFocused
                                }
                                .focusable(),
                        ) {
                           // Content here
                        }
                    }
                }
            }
        }
    }
}

显示阴影剪切问题的图像

Attempt 2: Modifier.drawBehind {}尝试 2:Modifier.drawBehind {}

I was referred to these lines in the Android code that limits elevation to 30.dp .在 Android 代码中提到了这些行,该代码将海拔高度限制为30.dp


val colors = listOf(
    Color.Red,
    Color.Blue,
    Color.Green,
    Color.Yellow
)

@Composable
fun ListTest() {
    LazyColumn {
        items(30) {
            Column {
                Text("This is row $it")
                LazyRow {
                    items(colors) {
                        var isFocused by remember { mutableStateOf(false) }
                        val alpha = if (isFocused) 1f else 0.25f
                        val shadowColor = if (isFocused) Color.Black else Color.Transparent
                        Surface(
                            shape = RoundedCornerShape(8.dp),
                            color = it.copy(alpha = alpha),
                            modifier = Modifier
                                .width(240.dp)
                                .height(150.dp)
                                .padding(start = 16.dp)
                                // 🔴 Look here
                                .coloredShadow(shadowColor)
                                .onFocusChanged { state ->
                                    isFocused = state.isFocused
                                }
                                .focusable(),
                        ) {
                            // Content here
                        }
                    }
                }
            }
        }
    }
}


fun Modifier.coloredShadow(color: Color) = drawBehind {
    val shadowColor = color.toArgb()
    val transparentColor = color.copy(alpha = 0f).toArgb()
    val offsetX = 0.dp
    val offsetY = 8.dp
    val cornerRadius = 4.dp
    drawIntoCanvas {
        val paint = Paint()
        val frameworkPaint = paint.asFrameworkPaint()
        frameworkPaint.color = transparentColor
        frameworkPaint.setShadowLayer(
            // 🔴 Set to 400.dp as radius
            400.dp.toPx(), 
            offsetX.toPx(),
            offsetY.toPx(),
            shadowColor
        )
        it.drawRoundRect(
            0f,
            0f,
            this.size.width,
            this.size.height,
            cornerRadius.toPx(),
            cornerRadius.toPx(),
            paint
        )
    }
}

第二次尝试后显示阴影剪切问题的图像

How can I get rid of this clipping issue?我怎样才能摆脱这个剪辑问题?

I don't think it's a clipping issue.我不认为这是剪辑问题。 You just have the elevation set too high, so the surface's shadow has to reach across another row/column, and display on top of another Surface, but because they're not children of the same view, it's not processing the shadow blurring properly.您只是将高度设置得太高,所以表面的阴影必须跨越另一行/列,并显示在另一个表面的顶部,但因为它们不是同一视图的子级,所以它无法正确处理阴影模糊。

Maybe you should try setting the elevation to 30dp or 24dp?也许您应该尝试将高度设置为 30dp 或 24dp?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动画 LazyColumn 和 LazyRow - Animated LazyColumn and LazyRow Jetpack Compose:嵌套的 LazyColumn / LazyRow - Jetpack Compose: nested LazyColumn / LazyRow LazyColumn 中的 LazyRow 每次都调用 recompose - LazyRow inside LazyColumn call recompose every time 如何在 Compose 中创建循环(无尽)Lazycolumn/LazyRow - How to create a circular (Endless) Lazycolumn/LazyRow in Compose 自定义 Compose Arrangement 以在 LazyRow/LazyColumn 的开头和结尾添加额外的间距 - Custom Compose Arrangement to add extra spacing at beginning and end of a LazyRow/LazyColumn 如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动? - How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose? 是否可以让 LazyRow/LazyColumn 在屏幕上显示一定数量的项目而不传递明确的 DP 宽度 - Is it possible to make LazyRow/LazyColumn display a certain amount of items on the screen without passing in an explicit DP width Android - Cardview 没有显示阴影,也没有剪裁儿童 - Android - Cardview not showing shadow & not clipping children as well 如何剪辑canvas.drawCircle()而不剪切阴影? - How to clip a canvas.drawCircle() without clipping the shadow? 浮动操作按钮在视图边缘的阴影剪裁 - Floating action button's shadow clipping at view margins
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM