9

I would like to set color programmatically to the progress bar primaryProgress, secondaryProgress as the color will be changed according to the background color of the screen.

Code:

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));

...

Edit:

** the color code here are for testing only. Afterwards the colorcode will be referenced to other part for updates accordingly

    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);

Question:

It underlines in red for primanyColor.setColor and reports that The method setColor(int, null) is undefined for the type Drawable .

How could I modify the above codes to make it works? Thanks!

4

3 回答 3

4

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

ProgressBar myProgress = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
myProgress.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
于 2017-07-25T08:59:32.933 回答
3

您可以做的是首先通过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 上声明原始可绘制对象的测量的最佳灵活性,之后无需修改其结构,特别是如果您需要特定于 xml 文件屏幕文件夹,然后只需通过代码。无需从头开始重新实例化新的 ShapeDrawable。

于 2016-06-30T14:50:50.303 回答
2

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

primaryColor.setColorFilter((Color.rgb(0,128,0)), 
                                                 PorterDuff.Mode.SRC_OVER);        
于 2015-07-03T07:13:50.273 回答