自定义环形进度条RoundProgressBar
一.效果图:

Canvas画圆环说明:
圆环宽度不必在意,只是画笔宽度设置后达到的效果.
二.实现步骤
1.自定义View-RoundProgressBar

2.设置属性resources(declear_styleable)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundProgressBar">
<attr name="maxProgress" format="integer"></attr>
<attr name="currentProgress" format="integer"></attr>
<attr name="roundColor" format="color"></attr>
<attr name="roundProgressColor" format="color"></attr>
<attr name="textColor" format="color"></attr>
<attr name="textProgress" format="integer"></attr>
<attr name="textSize" format="float"></attr>
<attr name="roundWidth" format="dimension"></attr>
<attr name="roundStyle" >
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr>
</declare-styleable>
</resources>
初始属性设置
public RoundProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
paint=new Paint();
TypedArray typedArray= context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);
maxProgress=typedArray.getInteger(R.styleable.RoundProgressBar_maxProgress,100);
roundColor=typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GRAY);
textColor=typedArray.getColor(R.styleable.RoundProgressBar_textColor,Color.RED);
textSize=typedArray.getFloat(R.styleable.RoundProgressBar_textSize,54);
roundWidth=typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth,30);
roundStyle=typedArray.getInteger(R.styleable.RoundProgressBar_roundStyle,0);
roundProgressColor=typedArray.getColor(R.styleable.RoundProgressBar_roundColor,Color.BLACK);
typedArray.recycle();
}
加载属性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="rgsc.roundprogressview.MainActivity">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
<rgsc.roundprogressview.RoundProgressBar
android:id="@+id/round_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_start"/>
</RelativeLayout>
控件布局
效果:

3.Canvas画图
画大圆环 canvas.drawCircle();

画进度百分比canvas.drawText():字体居中圆心显示的坐标=圆心坐标x-字体宽度/2;

画圆环进度canvas.drawArc(),RectF:RecF是画圆半径内切矩形的左上点坐标,及右下坐标;

package rgsc.roundprogressview; 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.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class RoundProgressBar extends View{
//自定义属性
int maxProgress; //最大进度值
int currentProgress; //当前进度
int roundColor; //圆环颜色
int roundProgressColor; //圆环进度颜色
int textColor; //字体颜色
int roundStyle; //圆环样式
float roundWidth; //圆环宽度
float textSize; //字号
Paint paint;
public RoundProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
paint=new Paint();
TypedArray typedArray= context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);
maxProgress=typedArray.getInteger(R.styleable.RoundProgressBar_maxProgress,100);
roundColor=typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GRAY);
textColor=typedArray.getColor(R.styleable.RoundProgressBar_textColor,Color.RED);
textSize=typedArray.getFloat(R.styleable.RoundProgressBar_textSize,54);
roundWidth=typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth,30);
roundStyle=typedArray.getInteger(R.styleable.RoundProgressBar_roundStyle,0);
roundProgressColor=typedArray.getColor(R.styleable.RoundProgressBar_roundColor,Color.BLACK);
typedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画最外层大圆环
float x=getWidth()/2;
float y=x;
float radius=x/2-roundWidth/2;
paint.setStyle(Paint.Style.STROKE); //空心画圆
paint.setColor(roundColor); //圆环颜色
paint.setStrokeWidth(roundWidth); //圆环宽度
canvas.drawCircle(x,y,radius,paint);//圆心坐标 (x,y)半径radius 画笔paint
//画进度百分比
paint.setColor(textColor);
paint.setTypeface(Typeface.DEFAULT_BOLD); //粗体
paint.setTextSize(textSize);
paint.setStrokeWidth(0);
int persentProgress=(int) (((float)currentProgress/(float)maxProgress)*100);
String text=persentProgress+"%";
float textWidth=paint.measureText(text); //获取字体宽度
float xText=x-textWidth/2; //减去字体宽度确保字体居中
canvas.drawText(text,xText,y,paint);
//画圆环进度
float left=x-radius;
float right=x+radius;
RectF rectF=new RectF(left,left,right,right);
paint.setStyle(Paint.Style.STROKE); //空心画圆
paint.setColor(roundProgressColor); //圆环颜色
paint.setStrokeWidth(roundWidth); //圆环宽度
Log.i("Round","画圆环进度:"+360*currentProgress/maxProgress);
canvas.drawArc(rectF,0,360*currentProgress/maxProgress,false,paint);
}
public synchronized void setCurrentProgress(int progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
if(progress > maxProgress){
progress = maxProgress;
}
if(progress <= maxProgress){
this.currentProgress = progress;
postInvalidate();
}
}
public synchronized int getCurrentProgress() {
return currentProgress;
}
public synchronized void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
public void setRoundColor(int roundColor) {
this.roundColor = roundColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public void setRoundProgressColor(int roundProgressColor) {
this.roundProgressColor = roundProgressColor;
}
public void setRoundStyle(int roundStyle) {
this.roundStyle = roundStyle;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
}
自定义环形进度条全部代码
4.动态进度设置
package rgsc.roundprogressview; import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button btn_start;
RoundProgressBar roundProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
}
void initControl(){
btn_start=(Button)findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
roundProgressBar=(RoundProgressBar)findViewById(R.id.round_progress);
//自定义属性设置
/*
roundProgressBar.setRoundColor(Color.GRAY);
roundProgressBar.setTextColor(Color.RED);
roundProgressBar.setRoundProgressColor(Color.BLACK);
roundProgressBar.setMaxProgress(100);
roundProgressBar.setRoundWidth(30);
roundProgressBar.setRoundStyle(0);
roundProgressBar.setTextSize(50);//*/
} @Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_start:
start();
break;
} }
void start(){
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<=100;i+=2){
roundProgressBar.setCurrentProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("Main","当前进度值:"+i);
}
}
}).start();
}
}
三.自定义属性效果


自定义环形进度条RoundProgressBar的更多相关文章
- Android简易实战教程--第十七话《自定义彩色环形进度条》
转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203533 点击打开链接 在Android初级教程里面,介绍了shape用法 ...
- iOS 开发技巧-制作环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...
- iOS一分钟学会环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现.先看一下这篇博客,博客地址:htt ...
- canvas绘制环形进度条
<!DOCTYPE html> <html > <head> <meta http-equiv="content-type" conten ...
- ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)
点显示进度条后→ android:max="100" 进度条的最大值 android:progress 进度条已经完成的进度值 android:progressDrawab ...
- canvas实现半圆环形进度条
html部分 <canvas id="canvas" width="150" height="150"> <p>抱歉 ...
- 仿MIUI音量变化环形进度条实现
Android中使用环形进度条的业务场景事实上蛮多的,比方下载文件的时候使用环形进度条.会给用户眼前一亮的感觉:再比方我大爱的MIUI系统,它的音量进度条就是使用环形进度条,尽显小米"为发烧 ...
- 用初中数学知识撸一个canvas环形进度条
周末好,今天给大家带来一款接地气的环形进度条组件vue-awesome-progress.近日被设计小姐姐要求实现这么一个环形进度条效果,大体由四部分组成,分别是底色圆环,进度弧,环内文字,进度圆点. ...
- 图解CSS3制作圆环形进度条的实例教程
圆环形进度条制作的基本思想还是画出基本的弧线图形,然后CSS3中我们可以控制其旋转来串联基本图形,制造出部分消失的效果,下面就来带大家学习图解CSS3制作圆环形进度条的实例教程 首先,当有人说你能不能 ...
随机推荐
- 清除DNS解析缓存
接下来在弹出的命令提示符窗口中输入“ipconfig /displaydns”,我们会看到系统中有多条我们之前使用过的DNS地址,如下图所示 5 然后,我们接着输入命令“ipconfig /flush ...
- MyEclipse Hibernate逆向工程的使用
简介MyEclipse自带很多非常实用的工具,本次将介绍Hibernate工具的使用.1.首先打开MyEclipse的Hibernate视图 2.然后在左上角的DB Browser视图中,右键,新建数 ...
- 多门店4s管理系统
下载 系统登录用户名与密码:manage/123456
- 认识python中的super函数
需求分析 在类继承中,存在这么一种情况: class Human(object): def Move(self): print("我会走路...") class Man(Human ...
- 二十三 NoSql&Redis及其安装
什么是Nosql not only sql , 不仅仅是sql,是一项全新的数据库理念,泛指非关系型的数据库. 为什么需要NoSql 解决以下问题: 1 High Performance 对数据库 ...
- C2 - Skyscrapers (hard version)
前几天做的题,当时好像是超时了,这个博客写的超好https://blog.csdn.net/lucky52529/article/details/89155694 用单调站解决问题. 代码是从另外一篇 ...
- 新闻网大数据实时分析可视化系统项目——17、Spark2.X分布式弹性数据集
1.三大弹性数据集介绍 1)概念 2)优缺点对比 2.Spark RDD概述与创建方式 1)概述 在集群背后,有一个非常重要的分布式数据架构,即弹性分布式数据集(resilientdistribute ...
- SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
[oracle@jtwy02 ~]$ sqlplus '/as sysdba' SQL*Plus: Release 11.2.0.4.0 Production on Sat Oct 13 14:14: ...
- redis 模糊查询与删除
创建一条数据 set name1 zhangsan 查询 get name1 在创建一条数据 set name2 lisi 查询 get name2 模糊查询 keys name* 查询结果 n ...
- 冰蝎动态二进制加密WebShell基于流量侧检测方案
概述 冰蝎是一款新型动态二进制加密网站工具.目前已经有6个版本.对于webshell的网络流量侧检测,主要有三个思路.一:webshell上传过程中文件还原进行样本分析,检测静态文件是否报毒.二:we ...