Android自定义圆形进度条
作者:银伙计 发布时间:2021-08-01 14:29:28
标签:android,进度条
今天小编来手写一个自定义圆形进度条:先看效果:
首先我们在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();
}
});
完结!


猜你喜欢
- 本文实例为大家分享了C#通过NPOI导入导出数据EXCEL的具体代码,供大家参考,具体内容如下其实从数据库到服务器导入导出有很多方法,但是比
- 很不错的蓝牙通信demo实现发送和接受功能,就用了两个类就实现了,具体内容如下说下思路把 主要有两个类 主界面类 和 蓝牙聊天服务类&nbs
- 由于我们在eclipse ee中把项目部署在web端经常会出现报404错误。原因为:404状态码是一种http状态码,其意思是: 所请求的页
- 在maven的pom.xml里面添加一下依赖:<properties><project.build.sourceEncod
- 完成支付宝支付、查询的接口之后,我们应该还需要定时与支付宝进行对账,以确保商户系统的订单信息是正确的,想知道支付宝支付、查询接口实现过程的亲
- 本文实例为大家分享了java http token的具体代码,供大家参考,具体内容如下package com.monitoring.comm
- 目录Bitmap类BitmapData类参考:Bitmap类Bitmap对象封装了GDI+中的一个位图,此位图由图形图像及其属性的像素数据组
- 本文为大家分享了Android中Drawable方法的详细使用方法,供大家参考,具体内容如下1. BitmapDrawable相关方法:新建
- 短网址(Short URL) ,顾名思义就是看起来很短的网址。自从twitter推出短网址服务以后,各大互联网公司都推出了自己的短网址服务。
- 下面是我的实现经历:1.首先,我是直接使用AlertDialog来实现,确定是,形状有点难看,而且获得Dialog里面的控件略显麻烦(因为我
- 闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能。于是乎开始学习.Net Remoting..Net Remoting 是由客户端通过R
- 注意:这篇博客已经和当前的分页插件完全不一样了,所以建议大家通过上面项目地址查看最新的源码和文档来了解。以前为Mybatis分页查询发愁过,
- 关于滑动冲突在Android开发中,如果是一些简单的布局,都很容易搞定,但是一旦涉及到复杂的页面,特别是为了兼容小屏手机而使用了Scroll
- 我们先回顾下,什么是指针?什么是常量?指针是一种特殊的变量,它里面存储的内容是内存地址。常量是指其里面存储的内容不能发生改变的量。明白了这两
- Java 是一门面向对象的编程语言,面向对象的编程语言有四大特征:抽象、封装、继承和多态。而本文介绍的接口和抽象类就是面向对象编程中“抽象”
- Spring Security提供如下几种认证机制Username & PasswordOAuth2.0 LoginSAML 2.0
- 自动编辑文本框(AutoCompleteTextView)继承自EditText,能够接受用户的输入编辑,但是有这自己的特色功能:输入一定的
- 上一篇文章谈到音频剪切、混音、拼接与转码,也详细介绍cMake配置与涉及FFmpeg文件的导入: android端采用FFmpeg进行音频混
- MAC设置JDK环境变量1、sudo vim ~/.bash_profile2、设置内容 (写自己的JDK路径,有两种方式查看)JAVA_H
- 方法一、利用控件或窗体的Paint事件中的PainEventArgs在窗体或控件的Paint事件中接收对图形对象的引用,作为PaintEve