Android Jetpack Compose CircularProgressIndicator 设置最小和最大进度

问题描述

我正在尝试设置 CircularProgressIndicator 的最小和最大进度,但我找不到任何解决方案。

这是给定的 Composable CircularProgressIndicator

@Composable
fun CircularProgressIndicator(
    /*@FloatRange(from = 0.0,to = 1.0)*/
    progress: Float,modifier: Modifier = Modifier,color: Color = MaterialTheme.colors.primary,strokeWidth: Dp = ProgressIndicatorDefaults.strokeWidth
) {
    val stroke = with(LocalDensity.current) {
        stroke(width = strokeWidth.toPx(),cap = strokeCap.Butt)
    }
    Canvas(
        modifier
            .progressSemantics(progress)
            .size(CircularIndicatorDiameter)
            .focusable()
    ) {
        // Start at 12 O'clock
        val startAngle = 270f
        val sweep = progress * 360f
        drawDeterminateCircularIndicator(startAngle,sweep,color,stroke)
    }
}

它只接受 0.0 到 1.0 的浮动范围内的进度,我想将其更改为接受从 0 到 100 的值。

注意:

我也发现了这个 Modifier.progressSemantics 但我不知道如何使用它来更改进度范围。

@Stable
fun Modifier.progressSemantics(
    value: Float,valueRange: ClosedFloatingPointRange<Float> = 0f..1f,/*@IntRange(from = 0)*/
    steps: Int = 0
): Modifier {
    val progress = (
        if (valueRange.endInclusive - valueRange.start == 0f) 0f
        else (value - valueRange.start) / (valueRange.endInclusive - valueRange.start)
        ).coerceIn(0f,1f)

    // We only display 0% or 100% when it is exactly 0% or 100%.
    val percent = when (progress) {
        0f -> 0
        1f -> 100
        else -> (progress * 100).roundToInt().coerceIn(1,99)
    }

    return semantics {
        stateDescription = "$percent percent"
        progressBarRangeInfo =
            ProgressBarRangeInfo(value.coerceIn(valueRange),valueRange,steps)
    }
}

感谢任何帮助。

解决方法

在您的情况下,您不需要更改进度范围。
只需将您的范围变成 0.0 - 1.0

只需将进度值除以 60000。
1000/60000 = 0.016 为每个刻度。

如果您想反转计数,只需使用以下内容:
val progress = 1 - (givenValueOnTick.toFloat / 60000)