如何在Android中更改进度条的进度颜色

598

我正在使用Android应用程序中的水平进度条,并且想要更改其进度颜色(默认为黄色)。如何在代码中完成此操作(不使用XML)?


你尝试过使用MyProgressBar.setProgressDrawable(Drawable d)方法指定一个具有所需颜色的位图吗?http://developer.android.com/reference/android/widget/ProgressBar.html#setProgressDrawable(android.graphics.drawable.Drawable) http://developer.android.com/reference/android/graphics/drawable/Drawable.html - fupsduck
2
是的,我已经尝试过了,但它并没有起作用。它会设置整个进度条视图的背景颜色,而不是仅设置进度条本身的背景颜色。谢谢。 - WhiteTigerK
75
android:indeterminateTint="@android:color/white" 只在 API >=21 版本及以上有效。 - Madeyedexter
1
当您使用非确定性时,android:progressTint="@android:color/white" - Kamil
42个回答

6

为了解释ateiob所问的内容,我在PaulieG的答案上增加了一些信息。我查看了当前版本的Android源代码,发现ProgressBar存在一个问题或优化,即如果你试图将进度设置为它已经达到的值,代码将忽略这个尝试,并且不会重新绘制进度条

  • 例如,如果进度为45,并且您尝试将其设置为45,则代码将不执行任何操作,也不会重新绘制进度

在调用ProgressBar.setProgressDrawable()后,您的进度条将变为空白的(因为您更改了可绘制部分)。

这意味着您需要设置进度并重新绘制它。但是,如果您只将进度设置为保留的值,则什么也不会发生。

您必须首先将其设置为0,然后再将其设置为“旧”值,进度条才会重新绘制。


因此,总结如下:

  • 保留“旧”的进度值
  • 更新可绘制部分/颜色(使进度条变空白)
  • 将进度重置为0(否则下一行不会执行任何操作)
  • 将进度重置为“旧”的值(修复进度条)
  • 无效化

下面是我的一个方法,可以做到以上这些:

protected void onResume()
{
    super.onResume();
    progBar = (ProgressBar) findViewById(R.id.progress_base);

    int oldProgress = progBar.getProgress();

    // define new drawable/colour
    final float[] roundedCorners = new float[]
        { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
        roundedCorners, null, null));
    String MyColor = "#FF00FF";
    shape.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);
    progBar.setProgressDrawable(clip);

    progBar.setBackgroundDrawable(getResources().getDrawable(
        android.R.drawable.progress_horizontal));

    // work around: setProgress() ignores a change to the same value
    progBar.setProgress(0);
    progBar.setProgress(oldProgress);

    progBar.invalidate();
}

就HappyEngineer的解决方案而言,我认为它是一种类似的解决方法,需要手动设置“进度”偏移量。无论哪种情况,上述代码都应该适用于您。

谢谢,这确实有效。但是因为我的drawable是静态的(来自资源),所以它不起作用。只有在我在“setProgress(0)”和“setProgressDrawable(drawable)”之前添加了“drawable.invalidateSelf();”后才起作用。 - Alécio Carvalho

5

如果有人想要通过编程方式更改颜色:

progressBar.progressTintList = ColorStateList.valueOf(Color.RED)

4
ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);

freeRamPb.getProgressDrawable().setColorFilter(
Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);

太棒了。快速有效的解决方案。谢谢! - Tobias Reich

4

这里是通过编程改变进度条颜色的代码。

ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));

4

将此自定义样式应用于进度条。

<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
        <item name="android:indeterminateDrawable">@drawable/progress</item>
        <item name="android:duration">40</item>
        <item name="android:animationCache">true</item>
        <item name="android:drawingCacheQuality">low</item>
        <item name="android:persistentDrawingCache">animation</item>
    </style>

@drawable/progress.xml -

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white"
    android:pivotX="50%"
    android:pivotY="50%" />

使用这种类型的图像来显示进度条。 enter image description here

为了获得更好的效果,您可以使用多个进度图像。请不要犹豫使用图像,因为Android平台本身就使用图像来显示进度条。 该代码是从SDK中提取的 :)


4

对于水平样式的进度条,我使用:

import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;

public void setColours(ProgressBar progressBar,
                        int bgCol1, int bgCol2, 
                        int fg1Col1, int fg1Col2, int value1,
                        int fg2Col1, int fg2Col2, int value2)
  {
    //If solid colours are required for an element, then set
    //that elements Col1 param s the same as its Col2 param
    //(eg fg1Col1 == fg1Col2).

    //fgGradDirection and/or bgGradDirection could be parameters
    //if you require other gradient directions eg LEFT_RIGHT.

    GradientDrawable.Orientation fgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;
    GradientDrawable.Orientation bgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;

    //Background
    GradientDrawable bgGradDrawable = new GradientDrawable(
            bgGradDirection, new int[]{bgCol1, bgCol2});
    bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
    bgGradDrawable.setCornerRadius(5);
    ClipDrawable bgclip = new ClipDrawable(
            bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
    bgclip.setLevel(10000);

    //SecondaryProgress
    GradientDrawable fg2GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg2Col1, fg2Col2});
    fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg2GradDrawable.setCornerRadius(5);
    ClipDrawable fg2clip = new ClipDrawable(
            fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Progress
    GradientDrawable fg1GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg1Col1, fg1Col2});
    fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg1GradDrawable.setCornerRadius(5);
    ClipDrawable fg1clip = new ClipDrawable(
            fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Setup LayerDrawable and assign to progressBar
    Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
    LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    //Copy the existing ProgressDrawable bounds to the new one.
    Rect bounds = progressBar.getProgressDrawable().getBounds();
    progressBar.setProgressDrawable(progressLayerDrawable);     
    progressBar.getProgressDrawable().setBounds(bounds);

    // setProgress() ignores a change to the same value, so:
    if (value1 == 0)
        progressBar.setProgress(1);
    else
        progressBar.setProgress(0);
    progressBar.setProgress(value1);

    // setSecondaryProgress() ignores a change to the same value, so:
    if (value2 == 0)
        progressBar.setSecondaryProgress(1);
    else
        progressBar.setSecondaryProgress(0);
    progressBar.setSecondaryProgress(value2);

    //now force a redraw
    progressBar.invalidate();
  }

一个示例调用是:
  setColours(myProgressBar, 
          0xff303030,   //bgCol1  grey 
          0xff909090,   //bgCol2  lighter grey 
          0xff0000FF,   //fg1Col1 blue 
          0xffFFFFFF,   //fg1Col2 white
          50,           //value1
          0xffFF0000,   //fg2Col1 red 
          0xffFFFFFF,   //fg2Col2 white
          75);          //value2

如果您不需要"次进度(secondary progress)",只需将"value2"设置为"value1"即可。

3
ProgressBar bar;

private Handler progressBarHandler = new Handler();

GradientDrawable progressGradientDrawable = new GradientDrawable(
        GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
                0xff1e90ff,0xff006ab6,0xff367ba8});
ClipDrawable progressClipDrawable = new ClipDrawable(
        progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] progressDrawables = {
        new ColorDrawable(0xffffffff),
        progressClipDrawable, progressClipDrawable};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);


int status = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO Auto-generated method stub
    setContentView(R.layout.startup);

    bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
    bar.setProgress(0);
    bar.setMax(100);

    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    bar.setProgressDrawable(progressLayerDrawable);
}

这帮助我通过代码将自定义颜色设置给进度条。希望能对你有所帮助。

3

如果您正在处理多样式应用程序,使用attr非常简单:

请尝试以下方法:

在attrs.xml中声明以下属性:

 <attr name="circularProgressTheme" format="reference"></attr>

将以下代码粘贴到styles.xml中:
 <style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
        <item name="colorAccent">#FF0000</item>
    </style>

    <style name="circularProgressThemeWhite">
        <item name="android:theme">@style/ProgressThemeWhite</item>
    </style>


  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">

   <item name="circularProgressTheme">@style/circularProgressThemeWhite</item>

 </style>

请使用下面的进度条:
  <ProgressBar
        style="?attr/circularProgressTheme"
        android:id="@+id/commonProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="visible"/>

2

由于方法public void setColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode)已被弃用,因此我使用了以下形式:

progressBar.indeterminateDrawable.colorFilter =
            PorterDuffColorFilter(ContextCompat.getColor(this, R.color.black), PorterDuff.Mode.SRC_IN)

2

水平进度条使用矩形形状的可绘制对象作为背景,使用从矩形形状构建的ClipDrawable表示进度(主要和次要)。色彩调整将颜色更改为某些色调。 如果您想要分别针对这三个元素定制颜色,则可以使用以下方法ProgressBar.setProgressDrawable()

    LayerDrawable progressBarDrawable = new LayerDrawable(
        new Drawable[]{
                new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                            new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),

                new ClipDrawable(
                            new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                    new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
                            Gravity.START,
                            ClipDrawable.HORIZONTAL),

                new ClipDrawable(
                        new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                                new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
                        Gravity.START,
                        ClipDrawable.HORIZONTAL)
            });

    progressBarDrawable.setId(0,android.R.id.background);
    progressBarDrawable.setId(1,android.R.id.secondaryProgress);
    progressBarDrawable.setId(2,android.R.id.progress);
    ((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);

请注意,在初始化LayerDrawable时,可绘制图层的顺序很重要。第一个可绘制图层应该是背景。根据我的实验,交换id是无效的。如果你为progressbar设置padding,则这种方法将不起作用。如果你需要padding,那么可以使用进度条的容器,例如LinearLayout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接