【发布时间】:2015-09-20 21:26:12
【问题描述】:
我想以编程方式为进度条设置颜色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。
如何修改上述代码以使其正常工作?谢谢!
【问题讨论】:
-
做
primaryColor.getPaint().setColor(Color.rgb(color_normal[0], color_normal[1], color_normal[2])); -
谢谢。我也尝试过,但出现错误:
The method getPaint() is undefined for the type Drawable -
@pearmak: ok 然后用
primaryColor.setColorFilter方法做 -
然后
ShapeDrawable primaryColor -
@ρяσѕρєяK:非常感谢!它现在正在工作!如果您在编辑部分发布答案会很高兴
标签: android colors android-progressbar layerdrawable