我在这里找到了一个主题的帮助,但是不能记住链接,我正在发布我的完整解决方案://Draw a simple progressBar from xml
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
//Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)
String color = colorDecToHex(75, 150, 160);
//Define a shape with rounded corners
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
//Sets the progressBar color
pgDrawable.getPaint().setColor(Color.parseColor(color));
//Adds the drawable to your progressBar
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
progressBar.setProgressDrawable(progress);
//Sets a background to have the 3D effect
progressBar.setBackgroundDrawable(Utils.getActivity().getResources()
. getDrawable(android.R.drawable.progress_horizontal));
//Adds your progressBar to your layout
contentLayout.addView(progressBar);
下面是将十进制颜色值转换为十六进制的代码:public static String colorDecToHex(int p_red, int p_green, int p_blue)
{
String red = Integer.toHexString(p_red);
String green = Integer.toHexString(p_green);
String blue = Integer.toHexString(p_blue);
if (red.length() == 1)
{
red ="0" + red;
}
if (green.length() == 1)
{
green ="0" + green;
}
if (blue.length() == 1)
{
blue ="0" + blue;
}
String colorHex ="#" + red + green + blue;
return colorHex;
}
我认为最后的方法不是那么干净,但效果很好。
希望这个帮助,太多时间浪费在这个progressbar上。