一、概述

  在上一篇博文中,我们给大家介绍了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. 教你如何取消GCD任务

    GCD 是一种非常方便的使用多线程的方式.通过使用 GCD,我们可以在确保尽量简单的语法的前提下进行灵活的多线程编程.在 "复杂必死" 的多线程编程中,保持简单就是避免错误的金科玉 ...

  2. (转载)JAVA中八种基本数据类型的默认值

    原文链接: http://simon-c.iteye.com/blog/1016031 引用 For type byte, the default value is zero, that is, th ...

  3. python编程快速上手之第4章实践项目参考答案

    #!/usr/bin/env python3.5 # coding:utf-8 # 假定有一个列表,编写函数以一个列表值作为参数,返回一个字条串 # 该字符串包含所有表项,之间以逗号和空格分隔,并在最 ...

  4. 多线程下System.Security.Cryptography.Aes CreateDecryptor报“Safe handle has been closed”的解决方案

    因为系统需要对一些核心数据进行预加载以保证查询速度. 所以在application_start 事件中启用了后台线程对相关的数据进行加载并解密(为了保证解密的效率,将AES对像做了静态对像来保存:pr ...

  5. Java电器商场小系统--简单的java逻辑

    //商场类public class Goods { int no; //编号 String name; //商品名称 double price; //商品价格 int number; //商品数量 / ...

  6. 猜年龄---while循环

    #!/usr/bin/env python# -*- coding:utf-8 -*-# Author:Andy Chen age_of_oldboy = 56 count = 0while True ...

  7. HTML5的应用缓存

    HTML5:提供一种应用缓存机制,使得基于web的应用程序可以离线运行.开发者可以使用  Application Cache (AppCache)  接口设定浏览器缓存的数据并使得数据离线有效. 在处 ...

  8. (转)$.extend()方法和(function($){...})(jQuery)详解

    1.    JS中substring与substr的区别 之前在项目中用到substring方法,因为C#中也有字符串的截取方法Substring方法,当时也没有多想就误以为这两种方法的使用时一样的. ...

  9. Maven “Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:create...”问题总结

    今天学习Maven的过程中,一直遇到一个问题:用maven指令构建新项目时,一直报错,用的 Maven 3.2 , JDK 6. 构建的命令: 错误信息: 解决方案: 在StackOverFlow上找 ...

  10. EF 数据库迁移(Migration)

    Update-Database -ConnectionStringName "MyConnectionString"