一.效果图:

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. Day12:看节目对于高考的想法

    今天看了一个科教频道的说高考的节目~ 老师问孩子的为什么要高考~ 学生说:为了以后不用辛苦,不用像爸妈一样再大冬天奔波,不辜负爸妈的期望~ 学习!高考!科举制度害了多少读书人?! 现在的高考跟科举有区 ...

  2. Ternsorflow 学习:004-MNIST入门 构建模型

    Softmax回归介绍 我们知道MNIST的每一张图片都表示一个数字,从0到9.我们希望得到给定图片代表每个数字的概率.比如说,我们的模型可能推测一张包含9的图片代表数字9的概率是80%但是判断它是8 ...

  3. 使用Zabbix监控Nginx服务实战案例

    使用Zabbix监控Nginx服务实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  一.编译安装nginx步骤详解并开启状态页 博主推荐阅读: https://www.cn ...

  4. maven安装和eclipse集成遇到的问题

    修改完maven的位置之后,修改配置文件conf/settings.xml <localRepository>E:/apache-maven-3.3.1-bin/mvn/mvnreposi ...

  5. python笔记10

    今日内容 参数 作用域 函数嵌套 知识点回顾 函数基本结果 def func(name,age,email): # 函数体(保持缩进一致) a = 123 print(a) return 1111#函 ...

  6. Vue(十)---路由

    Vue.js 路由允许我们通过不同的 URL 访问不同的内容.通过 Vue.js 可以实现多视图的单页Web应用(single page web application,SPA). 需要引入vue-r ...

  7. mysql sql语句不同平台上大小写区分

  8. SPI协议解析

    1. SPI物理层 SPI通讯需要使用4条线:3条总线和1条片选 . SPI遵循主从模式,3条总线分别是SCK.MOSI和MISO,片选线为nSS(低电平有效),SPI协议适用于一主多从的工作场景: ...

  9. 10 MySQL索引选择与使用

    索引概述     每种存储引擎对每个表至少支持16个索引,总索引长度至少256字节.     MyISAM和InnoDB的表默认创建BTREE索引.MEMORY引擎默认使用HASH索引,但也支持BTR ...

  10. 动态弹出框,iframe的name包含一串随机数

    由于name是随机的,要定位name比较困难,此处由于iframe的标签是唯一的,所以可以用tag_name定位 framename=driver.find_element_by_tag_name(& ...