编写自定义阴影在Android 6上不起作用

kyks70gy  于 2023-10-14  发布在  Android
关注(0)|答案(1)|浏览(135)

我创建自定义阴影与修改器扩展功能。它在其他Android版本上提供了完美的输出。但一些较旧的Android版本,如Android OS版本6,它没有显示任何阴影。修改器扩展函数看起来像这样:

fun Modifier.advancedShadow() = composed {
    val color = Color.SomeColor
    drawBehind {
        val shadowColor = color.copy(alpha = 0.10f).toArgb()
        val transparentColor = Color.Black.copy(alpha = 0f).toArgb()
        drawIntoCanvas {
            val paint = Paint()
            val frameworkPaint = paint.asFrameworkPaint()
            frameworkPaint.color = transparentColor
            frameworkPaint.setShadowLayer(
                12.dp,
                0.dp,
                1.dp,
                shadowColor
            )
            it.drawRoundRect(
                0f,
                0f,
                this.size.width,
                this.size.height,
                cornerRadius.toPx(),
                cornerRadius.toPx(),
                paint
            )
        }
    }
}
w6lpcovy

w6lpcovy1#

setShadowLayer()功能仅在启用硬件加速时才支持文本。
我使用androidx.compose.ui.draw中的Modifier.shadow(...)作为sdk < Q,它可以工作。它具有在旧设备上制作美丽阴影的所有参数!

@Stable fun Modifier.shadow(
    elevation: Dp,
    shape: Shape = RectangleShape,
    clip: Boolean = elevation > 0.dp,
    ambientColor: Color = DefaultShadowColor,
    spotColor: Color = DefaultShadowColor, 
)

相关问题