gpt4 book ai didi

android-jetpack-compose - Jetpack 撰写 : how to cut out card shape?

转载 作者:行者123 更新时间:2023-12-03 08:01:19 27 4
gpt4 key购买 nike

我想在相机预览上方创建一个半透明图层,如下所示:

enter image description here

我在我的应用程序中完成了相机预览,我想要的只是在预览上有一个半透明的图层,带有剪裁的卡片形状,如图所示(带有圆角)。

所以:全屏相机预览,上面有一个全屏半透明覆盖层,其中有一个卡片状的孔洞

我该怎么做?

最佳答案

您可以使用 BlendModes 从透明层中排除矩形

@Composable
private fun TransparentCamLayout() {
Box(
modifier = Modifier
.fillMaxSize()
.drawWithContent {

val canvasWidth = size.width
val canvasHeight = size.height
val width = canvasWidth * .9f
val height = width * 3 / 4f

drawContent()

drawWithLayer {

// Destination
// This is transparent color
drawRect(Color(0x99000000))

// Source
// This is where we extract this rect from transparent
drawRect(
topLeft = Offset((canvasWidth - width) / 2, canvasHeight * .3f),
size = Size(width, height),
color = Color.Transparent,
blendMode = BlendMode.SrcIn
)
}

drawRect(
topLeft = Offset((canvasWidth - width) / 2, canvasHeight * .3f),
size = Size(width, height),
color = Color.White,
style = Stroke(2.dp.toPx())
)
}
) {
Image(
modifier = Modifier.fillMaxSize(),
painter = painterResource(id = R.drawable.landscape5),
contentScale = ContentScale.Crop,
contentDescription = null
)
}
}

/**
* Draw with layer to use [BlendMode]s
*/
private fun DrawScope.drawWithLayer(block: DrawScope.() -> Unit) {
with(drawContext.canvas.nativeCanvas) {
val checkPoint = saveLayer(null, null)
block()
restoreToCount(checkPoint)
}
}

结果

enter image description here

在本教程的 BlendMode section 中你可以找到其他用途。如 this answer用于自定义剪辑,构建 rating bar并且有很多用途,限制你的想象力。 Blend 或 PorterDuff 模式对于构建自定义剪辑、Alpha 混合或像素操作非常有用。

关于android-jetpack-compose - Jetpack 撰写 : how to cut out card shape?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74074525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com