假设我在Compose中有以下代码.

一个背景洋红色圆圈,上面有一个默认黑色的文本.

enter image description here

我想将文本 colored颜色 混合为白色,当它在圆圈上方时.

Canvas(
    modifier = Modifier.size(256.dp),
    onDraw = {
        drawCircle(
            color = Color.Magenta,
            radius = 50f,
            center = Offset(
                x = size.width / 2,
                y = size.height  / 2),
        )
        val textSize = textMeasurer.measure(text = AnnotatedString("A"))
        drawText(
            textMeasurer = textMeasurer,
            text = "A",
            style = TextStyle(
                color = Color.Black,
                fontSize = 14.sp
            ),
            topLeft = Offset(
                x = size.width / 2 - 100f - (textSize.size.width / 2),
                y = size.height  / 2 - (textSize.size.height / 2)
            )
        )
        drawText(
            textMeasurer = textMeasurer,
            text = "B",
            style = TextStyle(
                color = Color.Black,
                fontSize = 14.sp
            ),
            topLeft = Offset(
                x = size.width / 2 - (textSize.size.width / 2),
                y = size.height  / 2 - (textSize.size.height / 2)
            )
        )
    }
)

我试过在Cirle上使用BlendMode,有很多不同的配置,但我无法让它工作.而draText没有我能看到的混合模式.

有没有一种简单的方法来实现混合,使圆圈上方的文本变白?

推荐答案

您需要设置要使用小于1f的blendMode的Composable的Alpha,或者使用要在其中绘制源和目标的层.降低Alpha会在引擎盖下增加一层,这两者基本上是一样的.

Jetpack Compose Applying PorterDuffMode to Image

How to clip or cut a Composable?

fun DrawScope.drawWithLayer(block: DrawScope.() -> Unit) {
    with(drawContext.canvas.nativeCanvas) {
        val checkPoint = saveLayer(null, null)
        block()
        restoreToCount(checkPoint)
    }
}

由于drawText没有blendMode参数,我们需要将其绘制为目标,还添加了动画来演示通过BlendMode进行的 colored颜色 变化.

enter image description here

@Composable
private fun BlendModeSample() {

    val textMeasurer = rememberTextMeasurer()

    val infiniteTransition = rememberInfiniteTransition()


    val offset by infiniteTransition.animateFloat(
        initialValue = -1f,
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 3000),
            repeatMode = RepeatMode.Reverse
        )
    )
    Canvas(
        modifier = Modifier.size(256.dp),
        onDraw = {

            val textSize = textMeasurer.measure(text = AnnotatedString("A"))
            drawText(
                textMeasurer = textMeasurer,
                text = "A",
                style = TextStyle(
                    color = Color.Black,
                    fontSize = 14.sp
                ),
                topLeft = Offset(
                    x = size.width / 2 - 100f - (textSize.size.width / 2),
                    y = size.height / 2 - (textSize.size.height / 2)
                )
            )


            drawWithLayer {

                // Destination
                drawText(
                    textMeasurer = textMeasurer,
                    text = "B",
                    style = TextStyle(
                        color = Color.Black,
                        fontSize = 14.sp
                    ),
                    topLeft = Offset(
                        x = size.width / 2 - (textSize.size.width / 2),
                        y = size.height / 2 - (textSize.size.height / 2) + offset * 100f
                    ),

                    )

                // Source
                drawCircle(
                    color = Color.Magenta,
                    radius = 50f,
                    center = Offset(
                        x = size.width / 2,
                        y = size.height / 2
                    ),
                    blendMode = BlendMode.SrcOut
                )
            }


        }
    )
}

你也可以使用混合模式来建立一个评级条.

Jetpack Compose: How to create a rating bar?

Android相关问答推荐

未解析的引用:背景 colored颜色

如何在点击按钮时将字符串插入到文本字段中的光标位置?

滑动以更改合成中的活动

合成 colored颜色 的GSON反序列化

Android 14无法删除已配置的文件

无法将非静态方法与Frida挂钩

MAP函数返回单位列表而不是字符列表

Android studio应用判断无法打开离线数据库

在移动设备上看到时如何增加 PasswordField 文本?

Jetpack 组合千位分隔符视觉转换,也适用于小数

如何在 BottomBar jetpack compose 中删除选定的椭圆项目 colored颜色

我的观点在jetpack compose中相互重叠

如何在行/列/卡片 compose 中添加左边框

Android Jetpack Compose - 每次文本字段值更改时,可组合函数都会重新组合

Android活动系统导航栏 colored颜色 ?

Android Jetpack Compose - 找不到 R.drawable?

如何在 Jetpack Compose 中擦除画布时变得透明,现在我得到白色?

Android Studio,Db 连接错误:发生异常情况导致驱动程序失败.请报告此异常

如何使在库范围之外无法访问的接口的具体实现.?

如何使 BasicTextField 上的光标以 jetpack compose 为中心?