【问题标题】:Android How to programmatically change progress drawable colorAndroid 如何以编程方式更改进度可绘制颜色
【发布时间】:2021-02-04 10:51:17
【问题描述】:

我的水平进度条有一个可绘制的自定义进度

下面是进度条:

<ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:progress="20"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"

下面是可绘制的进度条:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <stroke
                android:width="3px"
                android:color="#648A5E" />
            <corners android:radius="5dp"
                android:color="#d6f0d6"/>
            <gradient
                android:angle="270"
                android:centerColor="#d6f0d6"
                android:centerY="0.5"
                android:endColor="#d6f0d6"
                android:startColor="#d6f0d6" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <stroke
                    android:width="3px"
                    android:color="#648A5E" />
                <corners android:radius="5dp"
                    android:color="#d6f0d6"/>
                <gradient
                    android:angle="270"
                    android:centerColor="#d6f0d6"
                    android:centerY="0.5"
                    android:endColor="#d6f0d6"
                    android:startColor="#d6f0d6" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%">
            <shape>
                <stroke
                    android:width="3px"
                    android:color="#B1707C" />
                <corners android:radius="5dp"
                    android:color="#fce0e6"/>
                <gradient
                    android:angle="0"
                    android:endColor="#fce0e6"
                    android:startColor="#fce0e6" />
            </shape>
        </scale>
    </item>
</layer-list>

以下是我尝试以编程方式创建自定义进度可绘制的代码

Drawable bckgrndDr = new ColorDrawable(Color.parseColor("#e1203e"));
Drawable secProgressDr = new ColorDrawable(Color.parseColor("#eeeeee"));
Drawable progressDr = new ScaleDrawable(new ColorDrawable(Color.BLUE), Gravity.LEFT, 1, -1);
LayerDrawable resultDr = new LayerDrawable(new Drawable[] { bckgrndDr, secProgressDr, progressDr });
resultDr.setId(0, android.R.id.background);
resultDr.setId(1, android.R.id.secondaryProgress);
resultDr.setId(2, android.R.id.progress);
        
progressBar.setProgressDrawable(resultDr);
progressBar.setProgress(40);

我无法为 id 背景、secondaryProgress 和 progress 设置描边、角和渐变。 请建议我应该如何以编程方式创建和更改整个自定义进度可绘制的颜色。

【问题讨论】:

标签: android android-progressbar


【解决方案1】:

您可以将此示例用作进度条

    <ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_centerInParent="true"
    android:indeterminate="false"
    android:max="100"
    android:progress="0"
    android:progressDrawable="@drawable/custom_progressbar_drawable"
    android:secondaryProgress="0" />

然后为custom_progressbar_drawable创建一个drawable资源文件

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="-90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="270" >

<shape
    android:shape="ring"
    android:thickness="5.5dp"
    android:useLevel="false" >
    <gradient
        android:centerY="0.5"
        android:endColor="@color/grad"
        android:startColor="@color/light_blue"
        android:type="sweep"
        android:useLevel="false" />
</shape>

</rotate>

【讨论】:

  • 我需要以编程方式更改颜色,因为颜色来自服务器
猜你喜欢
  • 2012-07-07
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多