我正在使用Android应用程序中的水平进度条,并且想要更改其进度颜色(默认为黄色)。如何在代码
中完成此操作(不使用XML)?
我正在使用Android应用程序中的水平进度条,并且想要更改其进度颜色(默认为黄色)。如何在代码
中完成此操作(不使用XML)?
为了解释ateiob所问的内容,我在PaulieG的答案上增加了一些信息。我查看了当前版本的Android源代码,发现ProgressBar
存在一个问题或优化,即如果你试图将进度设置为它已经达到的值,代码将忽略这个尝试,并且不会重新绘制进度条。
在调用ProgressBar.setProgressDrawable()
后,您的进度条将变为空白的(因为您更改了可绘制部分)。
这意味着您需要设置进度并重新绘制它。但是,如果您只将进度设置为保留的值,则什么也不会发生。
您必须首先将其设置为0,然后再将其设置为“旧”值,进度条才会重新绘制。
因此,总结如下:
下面是我的一个方法,可以做到以上这些:
protected void onResume()
{
super.onResume();
progBar = (ProgressBar) findViewById(R.id.progress_base);
int oldProgress = progBar.getProgress();
// define new drawable/colour
final float[] roundedCorners = new float[]
{ 5, 5, 5, 5, 5, 5, 5, 5 };
ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
roundedCorners, null, null));
String MyColor = "#FF00FF";
shape.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
ClipDrawable.HORIZONTAL);
progBar.setProgressDrawable(clip);
progBar.setBackgroundDrawable(getResources().getDrawable(
android.R.drawable.progress_horizontal));
// work around: setProgress() ignores a change to the same value
progBar.setProgress(0);
progBar.setProgress(oldProgress);
progBar.invalidate();
}
如果有人想要通过编程方式更改颜色:
progressBar.progressTintList = ColorStateList.valueOf(Color.RED)
ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);
freeRamPb.getProgressDrawable().setColorFilter(
Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
这里是通过编程改变进度条颜色的代码。
ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
将此自定义样式应用于进度条。
<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
<item name="android:indeterminateDrawable">@drawable/progress</item>
<item name="android:duration">40</item>
<item name="android:animationCache">true</item>
<item name="android:drawingCacheQuality">low</item>
<item name="android:persistentDrawingCache">animation</item>
</style>
@drawable/progress.xml -
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_white"
android:pivotX="50%"
android:pivotY="50%" />
为了获得更好的效果,您可以使用多个进度图像。请不要犹豫使用图像,因为Android平台本身就使用图像来显示进度条。 该代码是从SDK中提取的 :)
对于水平样式的进度条,我使用:
import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
public void setColours(ProgressBar progressBar,
int bgCol1, int bgCol2,
int fg1Col1, int fg1Col2, int value1,
int fg2Col1, int fg2Col2, int value2)
{
//If solid colours are required for an element, then set
//that elements Col1 param s the same as its Col2 param
//(eg fg1Col1 == fg1Col2).
//fgGradDirection and/or bgGradDirection could be parameters
//if you require other gradient directions eg LEFT_RIGHT.
GradientDrawable.Orientation fgGradDirection
= GradientDrawable.Orientation.TOP_BOTTOM;
GradientDrawable.Orientation bgGradDirection
= GradientDrawable.Orientation.TOP_BOTTOM;
//Background
GradientDrawable bgGradDrawable = new GradientDrawable(
bgGradDirection, new int[]{bgCol1, bgCol2});
bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
bgGradDrawable.setCornerRadius(5);
ClipDrawable bgclip = new ClipDrawable(
bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
bgclip.setLevel(10000);
//SecondaryProgress
GradientDrawable fg2GradDrawable = new GradientDrawable(
fgGradDirection, new int[]{fg2Col1, fg2Col2});
fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
fg2GradDrawable.setCornerRadius(5);
ClipDrawable fg2clip = new ClipDrawable(
fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Progress
GradientDrawable fg1GradDrawable = new GradientDrawable(
fgGradDirection, new int[]{fg1Col1, fg1Col2});
fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
fg1GradDrawable.setCornerRadius(5);
ClipDrawable fg1clip = new ClipDrawable(
fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Setup LayerDrawable and assign to progressBar
Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
progressLayerDrawable.setId(2, android.R.id.progress);
//Copy the existing ProgressDrawable bounds to the new one.
Rect bounds = progressBar.getProgressDrawable().getBounds();
progressBar.setProgressDrawable(progressLayerDrawable);
progressBar.getProgressDrawable().setBounds(bounds);
// setProgress() ignores a change to the same value, so:
if (value1 == 0)
progressBar.setProgress(1);
else
progressBar.setProgress(0);
progressBar.setProgress(value1);
// setSecondaryProgress() ignores a change to the same value, so:
if (value2 == 0)
progressBar.setSecondaryProgress(1);
else
progressBar.setSecondaryProgress(0);
progressBar.setSecondaryProgress(value2);
//now force a redraw
progressBar.invalidate();
}
setColours(myProgressBar,
0xff303030, //bgCol1 grey
0xff909090, //bgCol2 lighter grey
0xff0000FF, //fg1Col1 blue
0xffFFFFFF, //fg1Col2 white
50, //value1
0xffFF0000, //fg2Col1 red
0xffFFFFFF, //fg2Col2 white
75); //value2
ProgressBar bar;
private Handler progressBarHandler = new Handler();
GradientDrawable progressGradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
0xff1e90ff,0xff006ab6,0xff367ba8});
ClipDrawable progressClipDrawable = new ClipDrawable(
progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] progressDrawables = {
new ColorDrawable(0xffffffff),
progressClipDrawable, progressClipDrawable};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
int status = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.startup);
bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
bar.setProgress(0);
bar.setMax(100);
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
progressLayerDrawable.setId(2, android.R.id.progress);
bar.setProgressDrawable(progressLayerDrawable);
}
如果您正在处理多样式应用程序,使用attr非常简单:
请尝试以下方法:
在attrs.xml中声明以下属性:
<attr name="circularProgressTheme" format="reference"></attr>
<style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">#FF0000</item>
</style>
<style name="circularProgressThemeWhite">
<item name="android:theme">@style/ProgressThemeWhite</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="circularProgressTheme">@style/circularProgressThemeWhite</item>
</style>
<ProgressBar
style="?attr/circularProgressTheme"
android:id="@+id/commonProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"/>
由于方法public void setColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode)
已被弃用,因此我使用了以下形式:
progressBar.indeterminateDrawable.colorFilter =
PorterDuffColorFilter(ContextCompat.getColor(this, R.color.black), PorterDuff.Mode.SRC_IN)
水平进度条
使用矩形形状
的可绘制对象作为背景,使用从矩形形状
构建的ClipDrawable
表示进度(主要和次要)。色彩调整将颜色更改为某些色调。 如果您想要分别针对这三个元素定制颜色,则可以使用以下方法ProgressBar.setProgressDrawable()
:
LayerDrawable progressBarDrawable = new LayerDrawable(
new Drawable[]{
new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ff0000ff"),Color.parseColor("#ff0000ff")}),
new ClipDrawable(
new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ff00ff00"),Color.parseColor("#ff00ff00")}),
Gravity.START,
ClipDrawable.HORIZONTAL),
new ClipDrawable(
new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffff0000"),Color.parseColor("#ffff0000")}),
Gravity.START,
ClipDrawable.HORIZONTAL)
});
progressBarDrawable.setId(0,android.R.id.background);
progressBarDrawable.setId(1,android.R.id.secondaryProgress);
progressBarDrawable.setId(2,android.R.id.progress);
((ProgressBar)findViewById(R.id.progressBar)).setProgressDrawable(progressBarDrawable);
LayerDrawable
时,可绘制图层的顺序很重要。第一个可绘制图层应该是背景。根据我的实验,交换id是无效的。如果你为progressbar
设置padding,则这种方法将不起作用。如果你需要padding,那么可以使用进度条的容器,例如LinearLayout
。
android:indeterminateTint="@android:color/white"
只在 API >=21 版本及以上有效。 - Madeyedexterandroid:progressTint="@android:color/white"
- Kamil