I'm implementing a gradient progress bar indicator.
I've found this useful which is drawing progress bar using drawLine like this,

drawLine(
    color = backgroundIndicatorColor,
    cap = StrokeCap.Round,
    strokeWidth = size.height,
    start = Offset(x = 0f, y = 0f),
    end = Offset(x = size.width, y = 0f)
)

Now I want to give it a custom rounded corner like below.
How can I do that?

推荐答案

您可以将drawRoundedRect函数用于任何CornerRadius,

enter image description here

@Preview
@Composable
private fun Test() {
    Canvas(
        modifier = Modifier
            .padding(16.dp)
            .fillMaxWidth()
            .height(30.dp)
    ) {

        val cornerRadius = 8.dp.toPx()
        val progress = .7f

        drawRoundRect(
            color = Color.LightGray,
            cornerRadius = CornerRadius(
                x = cornerRadius,
                y = cornerRadius
            )
        )
        drawRoundRect(
            brush = Brush.horizontalGradient(gradientColors),
            cornerRadius = CornerRadius(
                x = cornerRadius,
                y = cornerRadius
            ),
            size = Size(
                size.width * progress,
                size.height
            )
        )
    }
}

Android相关问答推荐

fillMaxHeight中的分数在列内不起作用(android jetpack compose)

安卓Gradle看不到dagger 柄

Android库中的kotlinCompilerExtensionVersion

为什么Ionic-checkbox不总是刷新Android上的视图?

在一列中垂直对齐两个框

插入视图模型时,dagger 未命中绑定错误

Jetpack Compose-如何在进入新产品线之前删除单词?

如何在Jetpack Compose中将对象的移动从一个路径平滑地切换到另一个路径?

Jetpack Compose X.dp 性能问题?

Android Studio 在 list 文件中已经声明了活动类,但仍出现无法找到明确的活动类的错误

需要在按钮 onclick 上从 string.xml 获取值. @Composable 调用只能在@Composable 函数的上下文中发生

如何在 Jetpack Compose 中的特定位置绘制图像

在 Jetpack Compose 中重用具有重复代码的列

如何解决 compose 中material 图标的由于接收器类型不匹配,以下候选者都不适用

Jetpack compose 为网络检索视频帧导致延迟

如何在 Dolphin 中启用 android studio new logcat | 2021.3.1 金丝雀 6?

如何使用 Kotlin 在 Android Wear(Galaxy watch 4)中继续在后台运行应用程序

房间实时数据:启动时崩溃

Android全屏AlertDialog

如何在 flow.stateIn() 之后从流中的另一个函数发出emits ?