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

时间:2015-07-03 06:28:56

标签: android colors android-progressbar layerdrawable

我想以编程方式将颜色设置为进度条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

我如何修改上述代码才能使其正常工作?谢谢!

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

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

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上声明原始drawable的测量结果,之后不会对其结构进行修改,特别是如果您需要具有特定的xml文件屏幕文件夹,那么只需修改通过代码颜色。无需从头开始重新实例化新的ShapeDrawable。

答案 2 :(得分:1)

以下是以编程方式为min sdk 21及更高版本更改ProgressBar颜色的代码

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