Android progressBar代码设置进度条颜色、背景颜色以及圆角显示

本文详细介绍了如何使用Java代码在Android应用中创建带有圆角的ProgressBar。通过GradientDrawable设置背景和进度条的颜色与圆角,再利用ClipDrawable控制进度显示区域,最终实现美观的圆角进度条效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下:

Java代码设置如下:

int roundRadius = 15; // 8dp 圆角半径 The x-radius of the oval used to round the corners

//准备progressBar带圆角的背景Drawable
GradientDrawable progressBg = new GradientDrawable();
//设置圆角弧度
progressBg.setCornerRadius(roundRadius); 
//设置绘制颜色
progressBg.setColor(getResources().getColor(R.color.color_efefef));

//准备progressBar带圆角的进度条Drawable
GradientDrawable progressContent = new GradientDrawable();
progressContent.setCornerRadius(roundRadius);
//设置绘制颜色,此处可以自己获取不同的颜色
progressContent.setColor(getColorForIndex(position));

//ClipDrawable是对一个Drawable进行剪切操作,可以控制这个drawable的剪切区域,以及相相对于容器的对齐方式
ClipDrawable progressClip = new ClipDrawable(progressContent, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Setup LayerDrawable and assign to progressBar
//待设置的Drawable数组
Drawable[] progressDrawables = {progressBg, progressClip};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
//根据ID设置progressBar对应内容的Drawable
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.progress);
//设置progressBarDrawable
progressBar.setProgressDrawable(progressLayerDrawable);

//设置进度值
progressBar.setProgress(item.getValue());

参考博客:

https://www.jianshu.com/p/c146419fc1b2

https://blog.youkuaiyun.com/buptlzx/article/details/8294979

https://blog.51cto.com/glblong/1739160

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值