在Android中仅通过代码更改进度条颜色

问题描述 投票:0回答:15

我有一个使用 ProgressBar 类的进度条。

就这样做:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

我需要使用输入值更改该颜色,如下所示:

int color = "red in RGB value".progressBar.setColor(color)

或者类似的东西...

我无法使用 XML 布局,因为进度条可供用户自定义。

android colors progress-bar
15个回答
65
投票

这将有很大帮助,不需要做那么多编码:)

ProgressBar spinner = new android.widget.ProgressBar(
            context,
            null,
            android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);

55
投票

更新

在较新版本的 Android(21 个版本)中,您可以通过编程方式更改进度条的颜色,只需使用

setProgressTintList

要将其设置为红色,请使用:

//bar is a ProgressBar
bar.setProgressTintList(ColorStateList.valueOf(Color.RED));

48
投票

如果您需要将背景和进度条着色为不同的颜色。

progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape android:shape="rectangle" >
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/green" />
            </shape>
        </clip>
    </item>
</layer-list>

可以通过编程方式分解其图层列表项,并分别对它们着色:

LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);
Drawable progressDrawable = progressBarDrawable.getDrawable(1);

backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN);
progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);

20
投票

当我在这里找到有关某个主题的帮助但不记得链接时,我发布了完整的解决方案,该解决方案非常适合我的需求:

    // Draw a simple progressBar from xml
    progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

    // Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)
    String color = colorDecToHex(75, 150, 160);

    // Define a shape with rounded corners
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners,     null, null));

    // Sets the progressBar color
    pgDrawable.getPaint().setColor(Color.parseColor(color));

    // Adds the drawable to your progressBar
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    progressBar.setProgressDrawable(progress);

    // Sets a background to have the 3D effect
    progressBar.setBackgroundDrawable(Utils.getActivity().getResources()
            .getDrawable(android.R.drawable.progress_horizontal));

    // Adds your progressBar to your layout
    contentLayout.addView(progressBar);

这里是将十进制颜色值转换为十六进制的代码:

public static String colorDecToHex(int p_red, int p_green, int p_blue)
{
    String red = Integer.toHexString(p_red);
    String green = Integer.toHexString(p_green);
    String blue = Integer.toHexString(p_blue);

    if (red.length() == 1)
    {
        red = "0" + red;
    }
    if (green.length() == 1)
    {
        green = "0" + green;
    }
    if (blue.length() == 1)
    {
        blue = "0" + blue;
    }

    String colorHex = "#" + red + green + blue;
    return colorHex;
}

我认为最后一种方法不太干净,但效果很好。

希望这能有所帮助,在这个进度条上浪费了太多时间。


16
投票

这对我来说适用于 AppCompat:

DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);

15
投票

可以通过在进度条可绘制对象上设置滤色器来对进度条进行着色:

Drawable drawable = progressBar.getProgressDrawable();
drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt));

15
投票

如果你想以编程方式更改进度条的颜色,那么你复制这段代码,它可以 100% 工作

 mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);   

10
投票

Kotlin 2021:

progressBar.indeterminateTintList = ColorStateList.valueOf(Color.WHITE)

您还可以使用 getColor() 传递资源

如果不是不确定的,您可能需要这 2 个:

setSecondaryProgressTintList

setProgressTintList

8
投票

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

仅适用于 API 21 以上


3
投票

我已经通过 drawablexml 中给出了 默认颜色

我已经以编程方式更改了它。

activity_splasg.xml

<ProgressBar
       android:id="@+id/splashProgressBar"
       android:progressDrawable="@drawable/splash_progress_drawable"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:max="100"
       android:progress="50"
       style="?android:attr/progressBarStyleHorizontal"
       android:layout_alignParentBottom="true" />

splash_progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid
                android:color="@android:color/transparent" />
        </shape>
    </item>

    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <solid
                    android:color="#e5c771" />
            </shape>
        </clip>
    </item>

</layer-list>

现在如何以编程方式更改ProgressDrawable 颜色

ProgressBar splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);

Drawable bgDrawable = splashProgressBar.getProgressDrawable();
bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);
splashProgressBar.setProgressDrawable(bgDrawable);

希望这对您有帮助。


1
投票

布局=activity_main.xml:

<ProgressBar
    android:id="@+id/circle_progress_bar_middle"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:max="100"
    android:rotation="-90"
    android:indeterminate="false"
    android:progressDrawable="@drawable/my_drawable_settings2" />

在 Java 活动/片段中:

ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle);
myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1));

drawable/mipmap 文件夹中的 my_drawable_settings1.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <shape
            android:innerRadius="55dp"
            android:shape="ring"
            android:thickness="9dp"
            android:useLevel="true">

            <gradient
                android:startColor="#3594d1"
                android:endColor="@color/white"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>

其中 my_drawable_settings1 和 my_drawable_settings2.xml 有不同的颜色。


0
投票
带有 2 个参数的

setColorFilter
已被弃用,另一个指向使用
LightingColorFilter
的答案对我来说都不起作用,所以

val progressBar = ProgressBar(context, null, android.R.attr.progressBarStyle).apply {

    val colorFilter = PorterDuffColorFilter(
        ContextCompat.getColor(context, R.color.yourColor),
        PorterDuff.Mode.MULTIPLY
    )

    indeterminateDrawable.colorFilter = colorFilter
}

这将以编程方式为您提供带有您的颜色的圆形进度条


0
投票

试试这个:

progress_wheel.getIndeterminateDrawable().setColorFilter(Color.parseColor(getPreferences().getString(Constant.SECOND_COLOR, Constant.SECONDARY_COLOR)), android.graphics.PorterDuff.Mode.MULTIPLY);

0
投票

您可以使用这个:

progressBar.indeterminateTintList = 
ColorStateList.valueOf(ContextCompat.getColor(context,R.color.white))

-1
投票

试试这个代码:

   CircularProgressIndicator  circularProgressIndicator = findViewById(R.id.tenant_progress_color);
    circularProgressIndicator.setIndicatorColor(color);
    circularProgressIndicator.setTrackColor(R.color.gray);
© www.soinside.com 2019 - 2024. All rights reserved.