ここでトピックに関するヘルプを見つけましたが、リンクを思い出せないので、私のニーズに最適な完全なソリューションを投稿しています:
// 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);
DECIMAL カラー値を HEXADECIMAL に変換するコードは次のとおりです。
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;
}
最後の方法はそれほどきれいではないと思いますが、うまく機能します。
このプログレスバーに無駄な時間がかかりすぎて、これがうまくいくことを願っています。