一.效果图:

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的更多相关文章

  1. Android简易实战教程--第十七话《自定义彩色环形进度条》

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203533   点击打开链接 在Android初级教程里面,介绍了shape用法 ...

  2. iOS 开发技巧-制作环形进度条

    有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...

  3. iOS一分钟学会环形进度条

    有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现.先看一下这篇博客,博客地址:htt ...

  4. canvas绘制环形进度条

    <!DOCTYPE html> <html > <head> <meta http-equiv="content-type" conten ...

  5. ProgressBar学习笔记,自定义横向进度条的样式(包含ActionBar上面的进度条)

     点显示进度条后→   android:max="100" 进度条的最大值 android:progress  进度条已经完成的进度值 android:progressDrawab ...

  6. canvas实现半圆环形进度条

    html部分 <canvas id="canvas" width="150" height="150"> <p>抱歉 ...

  7. 仿MIUI音量变化环形进度条实现

    Android中使用环形进度条的业务场景事实上蛮多的,比方下载文件的时候使用环形进度条.会给用户眼前一亮的感觉:再比方我大爱的MIUI系统,它的音量进度条就是使用环形进度条,尽显小米"为发烧 ...

  8. 用初中数学知识撸一个canvas环形进度条

    周末好,今天给大家带来一款接地气的环形进度条组件vue-awesome-progress.近日被设计小姐姐要求实现这么一个环形进度条效果,大体由四部分组成,分别是底色圆环,进度弧,环内文字,进度圆点. ...

  9. 图解CSS3制作圆环形进度条的实例教程

    圆环形进度条制作的基本思想还是画出基本的弧线图形,然后CSS3中我们可以控制其旋转来串联基本图形,制造出部分消失的效果,下面就来带大家学习图解CSS3制作圆环形进度条的实例教程 首先,当有人说你能不能 ...

随机推荐

  1. Hive的原生部署方式

    一.Hive的部署 1.官方文档 https://cwiki.apache.org/confluence/display/Hive/GettingStarted 2.前提条件 需要安装JDK1.7之上 ...

  2. mysql 安装完以后没有mysql服务

    用管理员身份打开命令控制台(cmd),然后将mysql的安装文件的路径打开(bin文件的路径),然后再路径下打上mysqld.exe -install, 会出现提示  Service successf ...

  3. python的super()以及父类继承

    Python中子类调用父类的方法有两种方法能够实现:调用父类构造方法,或者使用super函数(两者不要混用). 使用“super”时经常会出现代码“super(FooChild,self).__ini ...

  4. [Codeforces #608 div2]1272B Blocks

    Description There are nnn blocks arranged in a row and numbered from left to right, starting from on ...

  5. Golang gin开源实例——表设计

    UML Model 基本模型定义 type Model struct { ID int `gorm:"primary_key" json:"id"` Creat ...

  6. Scala match 变量

    昨天写 Scala 的时候,使用 match(相当于 switch)时,运行结果很奇怪. var i: Int = 0 while (i < items.length) { i % width ...

  7. 0106 springMVC REST风格

    markdown 印象笔记语法练习带快捷键的 加粗 快捷键 cmd+b 斜体 cmd+i 分割线 cmd+u 编号列表: cmd+shift+o 无编号列表 cmd+shift+u 待办事项 cmd+ ...

  8. javascript中退出语句break,continue和return 比较

    在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是返回的同时也将函数停止. 首先:break和continue两个 ...

  9. delphi关闭和禁用Windows服务

    function StopServices(const SvrName: string): Boolean; var SCH, SvcSCH: SC_HANDLE; SS: TServiceStatu ...

  10. 吴裕雄--天生自然java开发常用类库学习笔记:正则表达式

    public class RegexDemo01{ public static void main(String args[]){ String str = "1234567890" ...