android 改变进度颜色,仅在Android中通过CODE更改进度栏颜色

仅在Android中通过CODE更改进度栏颜色

我有一个使用ProgressBar类的progressBar。

只是这样做:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

我需要使用输入值更改该颜色,如下所示:

int color = "red in RGB value".progressBar.setColor(color)

或类似的东西...

我无法使用XML布局,因为进度栏是可为用户定制的。

11个解决方案

49 votes

这将不需要太多的编码即可:)

ProgressBar spinner = new android.widget.ProgressBar(

context,

null,

android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY);

Mit Bhatt answered 2019-12-31T23:17:26Z

33 votes

如果需要用不同的颜色给背景和进度栏着色。

progress_drawable.xml

可以通过程序分解其层列表项,并分别为其着色:

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);

brbsBruno answered 2019-12-31T23:17:55Z

19 votes

当我在这里找到有关某个主题的帮助但不记得链接时,我正在发布适用于我的需求的完整解决方案:

// 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;

}

我认为最后一种方法不是很干净,但是效果很好。

希望此功能对您有帮助,在此进度栏上浪费了太多时间。

hico answered 2019-12-31T23:18:28Z

18 votes

更新资料

在较新版本的Android(可运行21个作品)中,您只需使用setProgressTintList即可以编程方式更改进度条的颜色。

要将其设置为红色,请使用:

//bar is a ProgressBar

bar.setProgressTintList(ColorStateList.valueOf(Color.RED));

Jose M Vidal answered 2019-12-31T23:18:57Z

13 votes

可以通过在可绘制的进度条上设置滤色器来为进度条着色:

Drawable drawable = progressBar.getProgressDrawable();

drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt));

Moritz answered 2019-12-31T23:19:16Z

7 votes

这适用于我与AppCompat:DrawableCompat.setTint(progressBar.getProgressDrawable(),tintColor);

Lubos Ilcik answered 2019-12-31T23:19:36Z

7 votes

如果您想以编程方式更改进度条的颜色,则复制粘贴此代码即可正常工作100%

mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);

Pir Fahim Shah answered 2019-12-31T23:19:56Z

5 votes

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

仅适用于API 21以上

Daniel Park answered 2019-12-31T23:20:20Z

3 votes

我已经通过drawable在xml中给出了默认颜色。

我已通过编程方式对其进行了更改。

activity_splasg.xml:

android:id="@+id/splashProgressBar"

android:progressDrawable="@drawable/splash_progress_drawable"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:max="100"

android:progress="50"

style="?android:attr/progressBarStyleHorizontal"

android:layout_alignParentBottom="true" />

splash_progress_drawable.xml:

android:color="@android:color/transparent" />

android:id="@android:id/progress">

android:color="#e5c771" />

现在,如何以编程方式更改Progress Drawable颜色。

ProgressBar splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);

Drawable bgDrawable = splashProgressBar.getProgressDrawable();

bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY);

splashProgressBar.setProgressDrawable(bgDrawable);

希望这会帮助你。

Hiren Patel answered 2019-12-31T23:21:02Z

1 votes

这篇文章就是您想要的:如何在Android中更改进度条进度颜色

如果要用户选择自己的颜色,只需为每种颜色制作多个可绘制的XML文件,然后根据用户的选择进行选择。

Cruceo answered 2019-12-31T23:21:27Z

1 votes

布局= activity_main.xml:

android:id="@+id/circle_progress_bar_middle"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerInParent="true"

android:max="100"

android:rotation="-90"

android:indeterminate="false"

android:progressDrawable="@drawable/my_drawable_settings2" />

在Java活动/片段中:

ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle);

myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1));

drawable / mipmap文件夹中的my_drawable_settings1.xml文件:

android:innerRadius="55dp"

android:shape="ring"

android:thickness="9dp"

android:useLevel="true">

android:startColor="#3594d1"

android:endColor="@color/white"

android:type="sweep" />

其中my_drawable_settings1和my_drawable_settings2.xml具有不同的颜色。

Gene answered 2019-12-31T23:22:00Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值