android 如何在Jetpack排版中画出带线角端点的透明弧

3phpmpom  于 2023-02-14  发布在  Android
关注(0)|答案(1)|浏览(167)

我想绘制后,颜色渐变半径结束透明弧。这听起来令人困惑。我有进度条中,我已经结束了线在X的位置。之后,我想显示透明弧空间在X的位置与线渐变。我试图使用drawArc,但它不能正常工作。

@Composable
fun DrawProgressBar() {
    val activity = LocalContext.current as AppCompatActivity
    val rangeComposition = RangeComposition()
    val itemLst = rangeComposition.bpExplained
    val boxSize = 30.dp
    val brush = Brush.horizontalGradient(listOf(Color.Red, Color.Blue))
    val progressBarPointer = rangeComposition.findReadingWithPointer(142, 90).second
    Box(
        modifier = Modifier
            .background(Color.White)
            .height(height = boxSize)
    ) {
        Canvas(
            modifier = Modifier.fillMaxSize()
        ) {
            val strokeWidth = 8.dp
            val canvasWidth = size.width
            val canvasHeight = size.height
            val strokeWidthPx = density.run { strokeWidth.toPx() }
            drawLine(
                start = Offset(x = 0f, y = canvasHeight / 2),
                end = Offset(x = canvasWidth, y = canvasHeight / 2),
                color = Color.Gray,
                strokeWidth = strokeWidthPx,
                cap = StrokeCap.Round,
            )
            val progressBarPointerInPixel = (progressBarPointer / 100f) * canvasWidth
            activity.logE("progressBarPointerInPixel $progressBarPointerInPixel")
            drawLine(
                brush = brush,
                start = Offset(x = 0f, y = canvasHeight / 2),
                end = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2),
                strokeWidth = strokeWidthPx,
                cap = StrokeCap.Round,
            )
            drawArc(
                topLeft = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2),
                size = Size(8.dp.toPx(), strokeWidthPx),
                color = Color.Cyan,
                startAngle = -90f,
                sweepAngle = 180f,
                useCenter = true
            )
            itemLst.forEachIndexed { index, rangeItem ->
                val endPointInPixel = (rangeItem.endPoint / 100f) * canvasWidth
                if (index != itemLst.lastIndex) {
                    drawLine(
                        start = Offset(x = endPointInPixel, y = 0F),
                        end = Offset(x = endPointInPixel, y = boxSize.toPx()),
                        color = Color.Black,
                        strokeWidth = 4.dp.toPx(),
                    )
                }
            }
        }
    }
}
    • 实际产量**

    • 预期产出**

您可以找到RangeComposition.kt的源代码。

    • 更新**

在@GabrieleMariotti提到代码后,我尝试使用白色,我看到那里有一个白色的小竖条

fun DrawProgressBar() {
    val activity = LocalContext.current as AppCompatActivity
    val rangeComposition = RangeComposition()
    val itemLst = rangeComposition.bpExplained
    val boxSize = 30.dp
    val brush = Brush.horizontalGradient(listOf(Color.Red, Color.Blue))
    val progressBarPointer = rangeComposition.findReadingWithPointer(142, 90).second
    Box(
        modifier = Modifier
            .background(Color.White)
            .height(height = boxSize)
    ) {
        Canvas(
            modifier = Modifier.fillMaxSize()
        ) {
            val strokeWidth = 8.dp
            val canvasWidth = size.width
            val canvasHeight = size.height
            val strokeWidthPx = density.run { strokeWidth.toPx() }
            val pathEffect = PathEffect.dashPathEffect(floatArrayOf(canvasHeight / 19, canvasHeight / 19), 0f)
            drawLine(
                start = Offset(x = 0f, y = canvasHeight / 2),
                end = Offset(x = canvasWidth, y = canvasHeight / 2),
                color = Color.Gray,
                strokeWidth = strokeWidthPx,
                cap = StrokeCap.Round,
            )
            val progressBarPointerInPixel = (progressBarPointer / 100f) * canvasWidth
            drawLine(
                color = Color.White,
                start = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2),
                end = Offset(x = progressBarPointerInPixel + strokeWidthPx / 2, y = canvasHeight / 2),
                strokeWidth = strokeWidthPx,
            )
            drawLine(
                brush = brush,
                start = Offset(x = 0f, y = canvasHeight / 2),
                end = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2),
                strokeWidth = strokeWidthPx,
                cap = StrokeCap.Round,
            )
            drawArc(
                topLeft = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2 - strokeWidthPx / 2),
                size = Size(strokeWidthPx, strokeWidthPx),
                color = Color.White,
                startAngle = -90f,
                sweepAngle = 180f,
                useCenter = true
            )
            itemLst.forEachIndexed { index, rangeItem ->
                val endPointInPixel = (rangeItem.endPoint / 100f) * canvasWidth
                if (index != itemLst.lastIndex) {
                    drawLine(
                        start = Offset(x = endPointInPixel, y = 0F),
                        end = Offset(x = endPointInPixel, y = boxSize.toPx()),
                        color = Color.Black,
                        strokeWidth = 1.2.dp.toPx(),
                        pathEffect = pathEffect
                    )
                }
            }
        }
    }
}

结果

myss37ts

myss37ts1#

在你的弧中,你必须改变topLeft偏移,同时考虑strokeWidthPx引起的高度偏移。

topLeft = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2 - strokeWidthPx/2),

此外,由于圆角,您还应该添加一条从progressBarPointerInPixelprogressBarPointerInPixel + strokeWidthPx/2的线。
比如:

drawLine(
      //gray line
    )

    drawLine(
        color = Color.Cyan,
        start = Offset(x = progressBarPointerInPixel , y = canvasHeight / 2),
        end = Offset(x = progressBarPointerInPixel + strokeWidthPx/2, y = canvasHeight / 2),
        strokeWidth = strokeWidthPx,
    )

    drawLine(
        brush = brush,
        start = Offset(x = 0f, y = canvasHeight / 2),
        end = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2),
        strokeWidth = strokeWidthPx,
        cap = StrokeCap.Round,
    )

    drawArc(
        topLeft = Offset(x = progressBarPointerInPixel, y = canvasHeight / 2 - strokeWidthPx/2),
        size = Size(strokeWidthPx,strokeWidthPx),
        color = Color.Cyan,
        startAngle = -90f,
        sweepAngle = 180f,
        useCenter = true
    )

要实现透明弧,您可以将blendMode = BlendMode.DstOut添加到line+arc。它还需要将alpha !=1F应用到CanvasgraphicsLayer(alpha = 0.99f)检查doc了解有关blendMode的更多详细信息。

Canvas(
    modifier = Modifier.fillMaxSize().graphicsLayer(alpha = 0.99f)
    ) {
       
    drawLine(
       //gray line
    )

    
    drawLine(
        //...
        color = Color.Cyan,
        blendMode = BlendMode.DstOut
    )

    drawLine(
       //gradient
    )

    drawArc(
        blendMode = BlendMode.DstOut
    )

}

相关问题