Android:以编程方式将颜色设置为 ProgressBar

Android: set color programmatically to ProgressBar

我想以编程方式为进度条 primaryProgress、secondaryProgress 设置颜色,因为颜色会根据屏幕的背景颜色而改变。

代码:

LayerDrawable progressDrawable = (LayerDrawable) ProgressBar1.getProgressDrawable();
Drawable backgroundColor = progressDrawable.getDrawable(0);
Drawable secondaryColor = progressDrawable.getDrawable(1);
Drawable primaryColor = progressDrawable.getDrawable(2);

final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
primaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
secondaryColor = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));

primaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);
secondaryColor.setColor((Color.rgb(color_normal[0], color_normal[1], color_normal[2])), null);

progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));

...

编辑:

** 此处的颜色代码仅供测试。之后颜色代码将参考其他部分进行相应更新

    secondaryColor.setColorFilter((Color.rgb(255, 0, 0)), PorterDuff.Mode.SRC_OVER);
    primaryColor.setColorFilter((Color.rgb(0, 255, 213)), PorterDuff.Mode.SRC_OVER);        

    progressDrawable.setDrawableByLayerId(progressDrawable.getId(2), new ClipDrawable(primaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
    progressDrawable.setDrawableByLayerId(progressDrawable.getId(1), new ClipDrawable(secondaryColor, Gravity.LEFT, ClipDrawable.HORIZONTAL));
    ProgressBar1.setProgressDrawable(progressDrawable); 
    ProgressBar1.setProgress(progress);
    ProgressBar1.setSecondaryProgress(secondaryProgress);

问题:

primanyColor.setColor 为红色下划线并报告 The method setColor(int, null) is undefined for the type Drawable .

我怎样才能修改上面的代码使其工作?谢谢!

为 Drawable 使用设置颜色 Drawable.setColorFilter。喜欢:

primaryColor.setColorFilter((Color.rgb(0,128,0)), 
                                                 PorterDuff.Mode.SRC_OVER);        

你可以做的是首先通过xml使你的图层列表可绘制(意思是背景作为第一层,一个代表次要进度的可绘制作为第二层,另一个表示主要进度的可绘制对象作为最后一层),然后通过执行以下操作更改代码上的颜色:

public void setPrimaryProgressColor(int colorInstance) {
      if (progressBar.getProgressDrawable() instanceof LayerDrawable) {
        Log.d(mLogTag, "Drawable is a layer drawable");
        LayerDrawable layered = (LayerDrawable) progressBar.getProgressDrawable();
        Drawable circleDrawableExample = layered.getDrawable(<whichever is your index of android.R.id.progress>);
        circleDrawableExample.setColorFilter(colorInstance, PorterDuff.Mode.SRC_IN);
        progressBar.setProgressDrawable(layered);
    } else {
        Log.d(mLogTag, "Fallback in case it's not a LayerDrawable");
        progressBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
}

此方法将为您提供最大的灵活性,即在 xml 上声明原始可绘制对象的尺寸,之后不修改其结构,特别是如果您需要 xml特定于文件屏蔽文件夹,然后仅通过代码修改颜色。无需从头开始重新实例化新的 ShapeDrawable。

这是用于通过编程方式为 min sdk 版本 21 及更高版本更改 ProgressBar 颜色的代码

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