Android自定义圆形进度条
作者:银伙计 时间:2021-08-01 14:29:28
今天小编来手写一个自定义圆形进度条:先看效果:
首先我们在attrs属性文件中增加几个自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomProgressBar">
<!-- 圆形进度条进度显示的颜色 -->
<attr name="roundProgressColor" format="color"></attr>
<!-- 外圈圆的颜色 -->
<attr name="roundColor" format="color"></attr>
<!-- 圆的总宽度 -->
<attr name="roundWidth" format="dimension"></attr>
<!-- 字体显示的大小 -->
<attr name="textSize" format="dimension"></attr>
<!-- 字体显示的颜色-->
<attr name="textColor" format="color"></attr>
<!-- 进度的最大值 -->
<attr name="max" format="integer"></attr>
<!-- 是否显示文字 -->
<attr name="textShow" format="boolean"></attr>
</declare-styleable>
</resources>
上我们自定义类的实现代码:
package xxx.xxx.xxx;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import test.dn.com.dn_test.R;
/**
* Created by Administrator on 2017/5/16 0016.
*/
public class CircleProgressBar extends View {
private int max; //最大值
private int roundColor; //圆形进度条的颜色
private int roundProgressColor;//圆形进度条进度的颜色
private int textColor; //字体的颜色
private float textSize; //字体的大小
private float roundWidth; //圆的宽度
private boolean textShow; //是否显示圆
private int progress; //当前进度
private Paint mPaint; //画笔
public static final int STROKE = 0;
public static final int FILL = 1;
public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//初始化一只笔
mPaint = new Paint();
//获取xml当中设置的属性,如果没有设置,则设置一个默认值
TypedArray typedArray = context.obtainStyledAttributes(attrs , R.styleable.CustomProgressBar);
max = typedArray.getInteger(R.styleable.CustomProgressBar_max , 100);
roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.RED);
roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor , Color.BLUE);
textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor , Color.GREEN);
textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize , 55);
roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth , 10);
textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow , true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画背景圆环
int center = getWidth() / 2;
//设置半径
float radius = center - roundWidth / 2;
//设置圆圈的颜色
mPaint.setColor(roundColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(roundWidth);//圆环的宽度
mPaint.setAntiAlias(true);//设置抗锯齿
//画外圈
canvas.drawCircle(center , center ,radius , mPaint);
//画进度百分比
mPaint.setColor(textColor);
mPaint.setStrokeWidth(0);
//设置字体大小
mPaint.setTextSize(textSize);
mPaint.setTypeface(Typeface.DEFAULT);
//设置笔帽
mPaint.setStrokeCap(Paint.Cap.ROUND);
//设置文字的摆放方式为居中
mPaint.setTextAlign(Paint.Align.CENTER);
//获取当前进度的值
int percent = (int) (progress / (float)max * 100);
String strPercent = percent + "%";
//获取画笔的文字属性,总共有bottom , top , leading , ascent , descent 这个以后会详细讲解
Paint.FontMetricsInt fm = mPaint.getFontMetricsInt();
if(percent != 0){
canvas.drawText(strPercent , getWidth() / 2 ,
getWidth() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom, mPaint);
}
//画圆弧
RectF oval = new RectF(center - radius , center - radius ,center + radius , center + radius);
mPaint.setColor(roundProgressColor);
mPaint.setStrokeWidth(roundWidth);
mPaint.setStyle(Paint.Style.STROKE);
//设置笔帽
mPaint.setStrokeCap(Paint.Cap.ROUND);
//话进度
canvas.drawArc(oval , 0 , 360 * progress / max , false , mPaint);
}
public void setProgress(int progress){
if(progress < 0){
throw new IllegalArgumentException("进度progress不能小于0");
}
if(progress > max){
progress = max;
}
if(progress <= max){
this.progress = progress;
postInvalidate();
}
}
}
在我们的xml中设置控件:
<xxx.xxx.CircleProgressBar
android:id="@+id/progressbar"
android:layout_width="100dp"
android:layout_height="100dp"
app:roundProgressColor="#ff00ff"
app:textColor="#666666"
app:textSize="20dp"
app:roundWidth="15dp"
/>
Activity功能实现代码:
mProgressBar = (CircleProgressBar) findViewById(R.id.progressbar);
mProgressBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//模拟http请求
new Thread(new Runnable() {
@Override
public void run() {
while (progress <= 100){
progress += 2;
mProgressBar.setProgress(progress);
//模拟网络请求,每隔100毫秒增加一个进度
SystemClock.sleep(100);
}
}
}).start();
}
});
完结!
标签:android,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 中的 clone( ) 和 new哪个效率更高
2023-11-09 05:27:44
![](https://img.aspxhome.com/file/2023/6/59326_0s.png)
谈谈Java中自定义注解及使用场景
2022-08-28 04:45:39
![](https://img.aspxhome.com/file/2023/7/62717_0s.jpg)
SpringBoot 整合 Elasticsearch 实现海量级数据搜索功能
2021-09-23 02:38:51
C# dump系统lsass内存和sam注册表详细
2021-06-26 12:19:53
![](https://img.aspxhome.com/file/2023/9/70429_0s.jpg)
Android点亮屏幕或屏幕解锁和锁定以及其他相关权限实现代码
2021-12-21 16:44:26
MapTask工作机制图文详解
2021-12-13 11:03:27
![](https://img.aspxhome.com/file/2023/1/65771_0s.png)
Java五种方式实现多线程循环打印问题
2023-03-07 20:34:12
详谈Springfox与swagger的整合使用
2021-11-25 21:08:34
![](https://img.aspxhome.com/file/2023/5/77405_0s.png)
Android Studio gradle 编译提示‘default not found’ 解决办法
2023-07-19 09:23:06
基于Java向zip压缩包追加文件
2023-10-11 17:18:52
springboot整合mybatis-plus基于注解实现一对一(一对多)查询功能
2021-06-16 13:08:29
通过Docker启动Solace并在Spring Boot通过JMS整合Solace的操作方法
2023-07-11 09:52:28
![](https://img.aspxhome.com/file/2023/7/57507_0s.png)
教你怎么用Java开发扫雷游戏
2023-07-22 09:49:26
![](https://img.aspxhome.com/file/2023/0/63480_0s.png)
C++实现LeetCode(169.求大多数)
2023-09-04 08:03:47
Android 调用系统应用的方法总结
2023-05-05 09:53:52
浅谈Gradle 常用配置总结
2023-02-03 11:04:06
![](https://img.aspxhome.com/file/2023/1/137211_0s.png)
Android App调试内存泄露之Cursor篇
2023-11-22 03:36:29
关于Java从本地文件复制到网络文件上传
2022-05-10 16:49:02
![](https://img.aspxhome.com/file/2023/6/98126_0s.jpg)
Java使用Log4j记录日志的方法详解
2022-09-19 01:09:50
![](https://img.aspxhome.com/file/2023/6/78236_0s.png)
深入分析Android ViewStub的应用详解
2022-09-14 16:50:19
![](https://img.aspxhome.com/file/2023/7/130417_0s.gif)