hic*_*ico 56 android colors progress-bar
我有一个使用ProgressBar类的progressBar.
这样做:
progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
Run Code Online (Sandbox Code Playgroud)
我需要使用输入值来改变那个的颜色,如下所示:
int color = "red in RGB value".progressBar.setColor(color)
Run Code Online (Sandbox Code Playgroud)
或类似的东西...
我无法使用XML布局,因为进度条可以为用户自定义.
Mit*_*att 50
这将有助于更多的编码:)
ProgressBar spinner = new android.widget.ProgressBar(
context,
null,
android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
小智 35
如果您需要以不同颜色着色背景和进度条.
progress_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle" >
<solid android:color="@color/white" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="@color/green" />
</shape>
</clip>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
以编程方式可以分解其图层列表项,并分别对它们进行着色:
LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable backgroundDrawable = progressBarDrawable.getDrawable(0);
Drawable progressDrawable = progressBarDrawable.getDrawable(1);
backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN);
progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)
hic*_*ico 20
由于我在这里找到了关于某个主题的帮助但却记不起链接,我发布了我的完整解决方案,该解决方案非常适合我的需求:
// 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);
Run Code Online (Sandbox Code Playgroud)
以下是将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;
}
Run Code Online (Sandbox Code Playgroud)
我认为最后一种方法不是很干净但效果很好.
希望这很有帮助,在这个进度条上浪费了太多时间.
Jos*_*dal 19
更新
在较新版本的Android(21件作品)中,您只需使用即可以编程方式更改进度条的颜色setProgressTintList
.
要将其设置为红色,请使用:
//bar is a ProgressBar
bar.setProgressTintList(ColorStateList.valueOf(Color.RED));
Run Code Online (Sandbox Code Playgroud)
Mor*_*itz 13
可以通过在进度条drawable上设置颜色过滤器来对进度条颜色进行着色:
Drawable drawable = progressBar.getProgressDrawable();
drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt));
Run Code Online (Sandbox Code Playgroud)
Mer*_*n E 10
progressBar.indeterminateTintList = ColorStateList.valueOf(Color.WHITE)
Run Code Online (Sandbox Code Playgroud)
您还可以使用 getColor() 传递资源
如果不是不确定的话,您可能需要这两个:
setSecondaryProgressTintList
setProgressTintList
Run Code Online (Sandbox Code Playgroud)
如果您想以编程方式更改进度条的颜色,则可以复制此代码,使其可以正常工作100%
mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
53523 次 |
最近记录: |