當我發現一個主題的幫助在這裏,但不記得的聯繫,我張貼我的全解決方案,滿足我的需求的偉大工程:
// 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;
}
我認爲最後一種方法並不那麼幹淨,但是效果很好。
希望這個幫助很大,在這個進度條上浪費了太多時間。
幾乎可以工作,但如果你改變進度條的高度,背景可繪製將會太高 – kenyee