【问题标题】:Android Jetpack Compose CircularProgressIndicator Set Min and Max ProgressAndroid Jetpack Compose CircularProgressIndicator 设置最小和最大进度
【发布时间】:2021-03-11 04:33:45
【问题描述】:

我正在尝试设置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)
    }
}

感谢任何帮助。

【问题讨论】:

  • progressIndicator 没有标签。使用范围 0.0-1.0 或 0-100 是一样的。
  • @GabrieleMariotti 先生,抱歉,我没听懂。第一个CircularProgressIndicator 只在浮点数中取得进度,第二个如果我在progress 中给出的值大于1.0,它将填满我不想要的整个进度条。
  • 0-100变成0.0-1.0有什么问题?只需将您的值除以 100。
  • 我正在使用 CountDownTimer 类,每次 onTick 我收到 60000 个 1min = 60000 milis 间隔 1000 - 1000。例如:在 First Tick 我收到 = 59000 milis 我将其转换为 59000 / 1000 = 59 然后 59 / 100 = 0.59 然后设置进行中,这将填充第一个 Tick 的整个进度。
  • 如果您想反转进度,请使用:1 - (givenValueOnTick.toFloat / 60000)。刚刚回答了你的问题。

标签: android kotlin progress android-jetpack-compose


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多