一、概述

  在上一篇博文中,我们给大家介绍了Android自定义控件系列的基础篇。链接:http://www.cnblogs.com/jerehedu/p/4360066.html

  这一篇博文中,我们将在基础篇的基础上,再通过重写ondraw()方法和自定义属性实现圆形进度条,效果如图所示:

二、实现步骤

   1、  编写自定义组件MyCircleProgress扩展View

public class MyCircleProgress extends View {

}

  2、  在MyCircleProgress类中,定制属性

    public int progress  = 0;//进度实际值,当前进度
/**
* 自定义控件属性,可灵活的设置圆形进度条的大小、颜色、类型等
*/
private int mR;//圆半径,决定圆大小
private int bgColor;//圆或弧的背景颜色
private int fgColor;//圆或弧的前景颜色,即绘制时的颜色
private int drawStyle; //绘制类型 FILL画圆形进度条,STROKE绘制弧形进度条
private int strokeWidth;//STROKE绘制弧形的弧线的宽度
private int max;//最大值,设置进度的最大值
/**
* 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
*/
public synchronized void setProgress(int progress) {
if(progress<0){
progress=0;
}else if(progress>max){
progress=max;
}else{
this.progress = progress;
}
}
public int getMax() {
return max; }

  3、  为定制的属性编写attrs.xml资源,该资源文件放在res/values目录下,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleProgressBar">
<attr name="bgColor" format="color"/>
<attr name="fgColor" format="color"/>
<attr name="r" format="integer"/>
<attr name="strokeWidth" format="integer"/>
<attr name="drawStyle">
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr>
<attr name="max" format="integer"/>
</declare-styleable>
</resources>

  4、  在MyCircleProgress类中定义构造函数,初始化属性

    private void initProperty(AttributeSet attrs){
TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
mR=tArray.getInteger(R.styleable.CircleProgressBar_r,10);
bgColor=tArray.getColor(R.styleable.CircleProgressBar_bgColor, Color.GRAY);
fgColor=tArray.getColor(R.styleable.CircleProgressBar_fgColor, Color.RED);
drawStyle=tArray.getInt(R.styleable.CircleProgressBar_drawStyle, 0);
strokeWidth=tArray.getInteger(R.styleable.CircleProgressBar_strokeWidth, 10);
max=tArray.getInteger(R.styleable.CircleProgressBar_max, 100);
}
public MyCircleProgress(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.paint = new Paint();
this.paint.setAntiAlias(true); // 消除锯齿
this.paint.setStyle(Style.STROKE); // 绘制空心圆或 空心矩形
initProperty(attrs);
}

  5、  在MainActivity中布局文件中添加MyCircleProgress组件,如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.jereh.mydrawcircleprogress"
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=".MainActivity"
>
<com.jereh.views.MyCircleProgress
android:id="@+id/MyCircleProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:r="45"
app:strokeWidth="10"
app:bgColor="#cccccc"
app:fgColor="#ff0000"
app:drawStyle="FILL"
app:max="50"
/>
</RelativeLayout>

  6、  自定义组件MyCircleProgress中重写onDraw方法:

    protected  void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth() / 2; // 圆心位置
this.paint.setColor(bgColor);
this.paint.setStrokeWidth(strokeWidth);
canvas.drawCircle(center, center, mR, this.paint);
// 绘制圆环
this.paint.setColor(fgColor);
if(drawStyle==0){
this.paint.setStyle(Style.STROKE);
opt=false;
}else{
this.paint.setStyle(Style.FILL);
opt=true;
}
int top = (center - mR);
int bottom = (center + mR);
RectF oval = new RectF(top, top, bottom, bottom);
canvas.drawArc(oval, 270, 360*progress/max, opt, paint); }

  7、编写MainActivity

public class MainActivity extends Activity {
private MyCircleProgress progressView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressView = (MyCircleProgress) findViewById(R.id.MyCircleProgress);
new ProgressAnimation().execute();
}
class ProgressAnimation extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
//进度值不断的变化
for (int i = 0; i < progressView.getMax(); i++) {
try {
publishProgress(i);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
} @Override
protected void onProgressUpdate(Integer... values) {
//更新进度值
progressView.setProgress(values[0]);
progressView.invalidate();
super.onProgressUpdate(values);
}
}
}

Android自定义控件系列之应用篇——圆形进度条的更多相关文章

  1. Android 高手进阶,自己定义圆形进度条

    背景介绍 在Android 开发中,我们常常遇到各种各样绚丽的控件,所以,依靠我们Android本身所带的控件是远远不够的,许多时候须要我们自定义控件,在开发的过程中.我们公司遇到了一种须要自己写的一 ...

  2. Android自定义控件系列之基础篇

    一.概述 在android开发中很多UI控件往往需要进行定制以满足应用的需要或达到更加的效果,接下来就通过一个系列来介绍自定义控件,这里更多是通过一些案例逐步去学习,本系列有一些典型的应用,掌握好了大 ...

  3. Android 三种方式实现自定义圆形进度条ProgressBar

    一.通过动画实现 定义res/anim/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...

  4. Qt自定义控件系列(一) --- 圆形进度条

    本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...

  5. android 自定义控件——(四)圆形进度条

    ----------------------------------↓↓圆形进度条(源代码下有属性解释)↓↓---------------------------------------------- ...

  6. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明地址:http://blog.csdn.net/xiaanming/article/detail ...

  7. Android 带进度的圆形进度条

    最近项目有个需求,做带进度从下到上的圆形进度条. 网上查了一下资料,发现这篇博客写得不错http://blog.csdn.net/xiaanming/article/details/10298163 ...

  8. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  9. 【Android 应用开发】 自定义 圆形进度条 组件

    转载著名出处 : http://blog.csdn.net/shulianghan/article/details/40351487 代码下载 : -- CSDN 下载地址 : http://down ...

随机推荐

  1. 利用libcurl进行post

    逛百度知道看到有个人提问:http://zhidao.baidu.com/question/1447092283140740700 C写HTTP应用只有疯子才会老老实实的SOCKET编程吧?我后来还是 ...

  2. 【Python3之pymysql模块】

    一.什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Python 数据库 ...

  3. 设备常用框架framework

    framework名称 framework说明 framework文档 Accelerate.framework 包含加速数学和DSP函数 http://developer.apple.com/iph ...

  4. C# 中的 ConfigurationManager类引用方法应用程序配置文件App.config的写法

    c#添加了Configuration;后,竟然找不到 ConfigurationManager 这个类,后来才发现:虽然引用了using System.Configuration;这个包,但是还是不行 ...

  5. CSS 样式书写规范+特殊符号

    虽然我只是刚踏入web前端开发圈子.在一次次任务里头,我发觉每一次的css命名都有所不同和不知所措.脑海就诞生了一个想法--模仿大神的css命名样式. 毕竟日后工作上,是需要多个成员共同协作的.如果没 ...

  6. 初学angular

    1.angular   表达式 2.ng-app   ng-init  ng-model  ng-repeat ng-model是用于表单元素的,支持双向绑定.对普通元素无效: ng-bind用于普通 ...

  7. jmeter问题处理随笔1 - CSV取值数据异常处理(包含"号,","号的情况)

    背景 jmeter测试中通过CSV进行用例数据的管理,在result断言中间需要使用json格式的数据,会包含 " ",",这个时候发现CSV取值会报错或者乱码 解决 用 ...

  8. maven创建web工程Spring配置文件找不到问题解决方案

    使用maven创建web工程,将Spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...

  9. (转)HashMap深入原理解析

    [HashMap]深入原理解析 分类: 数据结构 自考 equals与“==”(可以参考自己的另一篇博文) 1,基本数据类型(byte,short,char,int,long,float,double ...

  10. C语言基础 - 实现动态数组并增加内存管理

    用C语言实现一个动态数组,并对外暴露出对数组的增.删.改.查函数 (可以存储任意类型的元素并实现内存管理) 这里我的编译器就是xcode 分析: 模拟存放 一个 People类 有2个属性 字符串类型 ...