【问题标题】:How to recreate the progress bar rotation?如何重新创建进度条旋转?
【发布时间】:2019-11-27 01:49:41
【问题描述】:

如何重新创建 ProgressBar 类型的 progressBarStyleLarge 的旋转?

我有这个布局:

<androidx.core.widget.ContentLoadingProgressBar
    android:id="@+id/loadingIndicator"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:indeterminateDrawable="@drawable/loadingindicator"
    style="?android:attr/progressBarStyleLarge" />

可绘制loadingindicator:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="0"
    android:toDegrees="360" >

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="40"
        android:useLevel="false" >

        <size
            android:height="48dip"
            android:width="48dip" />

        <gradient
            android:startColor="#ff00ff"
            android:centerColor="#ff00ff"
            android:endColor="#ffffff"
            android:centerY="0.50"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

但这会导致平滑的恒定旋转,而不是 - 我相信 - 以默认样式显示的 2 个平行旋转。

具体我想重新创建Indeterminate circular progress indicator的动画,如下图: https://material.io/components/progress-indicators/#circular-progress-indicators

XY 问题披露: 我需要这个,因为我想自定义 ProgressBar 的笔画粗细和外边距。

【问题讨论】:

    标签: android android-progressbar


    【解决方案1】:

    您可以在如下代码中使用CircularProgressDrawable

    val drawable = CircularProgressDrawable(context).apply {
        strokeWidth = // desired width in pixels
    }
    progressBar.indeterminateDrawable = drawable
    

    文档: https://developer.android.com/reference/android/support/v4/widget/CircularProgressDrawable

    【讨论】:

    • 另外,如果你想学习如何自己制作动画,这里是我几年前写的一篇关于这个主题的博文的无耻插件:medium.com/@patrick_iv/…
    • 快速跟进:如何在该drawable中以编程方式更改笔触颜色?
    • 如果我没记错的话,你可以调用drawable.setColorSchemeColors(color),颜色为ARGB值
    • 你是对的!旁注:设置多种颜色会使可绘制对象遍历颜色,就像 Google 的多色微调器动画一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2014-02-15
    • 2012-05-04
    相关资源
    最近更新 更多