源代码及可执行文件下载地址:http://files.cnblogs.com/rainboy2010/ProgressChart.zip

因项目需要,自己尝试定义了一个进度环,用于显示进度,实现效果如下:

主要代码如下:

  1. public class ProgressChart extends View
  2. {
  3.  
  4. private Context context;
  5.  
  6. //圆环背景画笔
  7. private Paint paintBg;
  8.  
  9. //圆环进度画笔
  10. private Paint paintProgress;
  11.  
  12. //文字画笔1
  13. private Paint paintText1;
  14.  
  15. //文字画笔2
  16. private Paint paintText2;
  17.  
  18. // 圆环的宽度
  19. private float progressWidth;
  20.  
  21. //圆环的区域
  22. private RectF roundRect;
  23.  
  24. //文字的区域
  25. private Rect textRect;
  26.  
  27. //单个字符的区域
  28. private Rect charRect;
  29.  
  30. //绘制时每次增加的度数
  31. private float rotateDegree = 1.0F;
  32.  
  33. //绘制开始的度数
  34. private float startDegree = 0.0F;
  35.  
  36. //结束的度数
  37. private float endDegree;
  38.  
  39. //背景颜色
  40. private int bgColor;
  41.  
  42. //进度颜色
  43. private int progressColor;
  44.  
  45. // 中间进度百分比的字符串的颜色
  46. private int textColor;
  47.  
  48. //字符串的文字大小
  49. private float text1Size;
  50.  
  51. //字符串的文字大小
  52. private float text2Size;
  53.  
  54. //绘制的字符串
  55. private String text="0%";
  56.  
  57. public ProgressChart(Context context)
  58. {
  59. this(context, null);
  60. }
  61.  
  62. public ProgressChart(Context context, AttributeSet attrs)
  63. {
  64. this(context, attrs, 0);
  65. }
  66.  
  67. public ProgressChart(Context context, AttributeSet attrs,int defStyleAttr)
  68. {
  69. super(context, attrs, defStyleAttr);
  70.  
  71. this.context=context;
  72.  
  73. init(attrs);
  74. }
  75.  
  76. private void init(AttributeSet attrs)
  77. {
  78. TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.ProgressChart);
  79.  
  80. this.progressWidth = mTypedArray.getDimension(R.styleable.ProgressChart_progressWidth, 8);
  81. this.text1Size = mTypedArray.getDimension(R.styleable.ProgressChart_textSize1, 32);
  82. this.text2Size = mTypedArray.getDimension(R.styleable.ProgressChart_textSize2, 20);
  83. this.bgColor = mTypedArray.getColor(R.styleable.ProgressChart_bgColor,Color.parseColor("#fff2f2f2"));
  84. this.progressColor = mTypedArray.getColor(R.styleable.ProgressChart_progressColor,Color.parseColor("#fffd0000"));
  85. this.textColor = mTypedArray.getColor(R.styleable.ProgressChart_txtColor,Color.parseColor("#fffd0000"));
  86.  
  87. mTypedArray.recycle();
  88.  
  89. this.textRect = new Rect();
  90. this.charRect = new Rect();
  91.  
  92. this.paintBg = new Paint();
  93. this.paintBg.setStyle(Paint.Style.STROKE);
  94. this.paintBg.setStrokeWidth(this.progressWidth);
  95. this.paintBg.setColor(this.bgColor);
  96.  
  97. this.paintProgress = new Paint();
  98. this.paintProgress.setStyle(Paint.Style.STROKE);
  99. this.paintProgress.setStrokeWidth(this.progressWidth);
  100. this.paintProgress.setColor(this.progressColor);
  101.  
  102. this.paintText1 = new Paint();
  103. this.paintText1.setTextSize(this.text1Size);
  104. this.paintText1.setTextAlign(Paint.Align.CENTER);
  105. this.paintText1.setColor(this.textColor);
  106.  
  107. this.paintText2 = new Paint();
  108. this.paintText2.setTextSize(this.text2Size);
  109. this.paintText2.setTextAlign(Paint.Align.CENTER);
  110. this.paintText2.setColor(this.textColor);
  111. }
  112.  
  113. public boolean setProgress(String progress)
  114. {
  115. this.text = DecimalFormat.getPercentInstance().format(Double.valueOf(progress));
  116. this.startDegree = 0.0F;
  117. this.endDegree = (360.0F * Float.valueOf(progress).floatValue());
  118. this.rotateDegree = (this.endDegree / 40.0F);
  119.  
  120. invalidate();
  121.  
  122. return true;
  123. }
  124.  
  125. @Override
  126. protected void onDraw(Canvas canvas)
  127. {
  128. //绘制圆环背景
  129. canvas.drawArc(this.roundRect, 0.0F, 360.0F, false, this.paintBg);
  130.  
  131. //绘制进度
  132. if (this.startDegree < this.endDegree)
  133. {
  134. canvas.drawArc(this.roundRect, -90.0F, this.startDegree, false, this.paintProgress);
  135. this.startDegree += this.rotateDegree;
  136. invalidate();
  137. }
  138. else
  139. {
  140. canvas.drawArc(this.roundRect, -90.0F, this.endDegree, false, this.paintProgress);
  141. }
  142.  
  143. if(!TextUtils.isEmpty(this.text))
  144. {
  145. //绘制文字
  146. this.paintText1.getTextBounds(this.text, 0, this.text.length(), this.textRect);
  147. this.paintText2.getTextBounds("%", 0, 1, this.charRect);
  148.  
  149. FontMetricsInt fontMetricsInt = this.paintText1.getFontMetricsInt();
  150. float y = this.roundRect.top + (this.roundRect.bottom - this.roundRect.top - fontMetricsInt.bottom + fontMetricsInt.top) / 2.0F - 5 * fontMetricsInt.top / 5;
  151. canvas.drawText(this.text.replace("%", ""), this.roundRect.centerX() - this.charRect.width() / 2, y, this.paintText1);
  152. canvas.drawText("%", this.roundRect.centerX() + this.textRect.width() / 2 - this.charRect.width() / 2, y, this.paintText2);
  153. }
  154. }
  155.  
  156. @Override
  157. protected void onSizeChanged(int w, int h, int oldw, int oldh)
  158. {
  159. this.roundRect = new RectF(this.progressWidth, this.progressWidth, w - this.progressWidth, h - this.progressWidth);
  160. }
  161.  
  162. }

Android一个自定义的进度环:ProgressChart的更多相关文章

  1. 使用VB6写一个自定义的进度信息框窗口

    一.起因说明 之前有些项目是用Access完成的,当时为了给用户显示一些进度信息,自制了一个进度信息窗体,类似下图所示: 随着项目不断变迁,需要将进度信息按阶段及子进度进行显示,并且出于代码封装的需求 ...

  2. 【Winform-自定义控件】一个自定义的进度条

    0.选择基类 public class MySlider : Control 1.设置控件的Style 在构造函数里添加: public MySlider() { //1.设置控件Style this ...

  3. HTML5简单进度环插件

    前几天做了一个进度条的插件.今天我用HTML5的arc做一个简单的进度环的插件. 代码演示 事实上非常easy的.相同,我们先用一个实例: 配置js代码 var setting = { id: &qu ...

  4. Android开发 View_自定义圆环进度条View

    前言 一个实现,空心圆环的自定义View,已经封装完好,可以直接使用. 效果图 代码 import android.content.Context; import android.graphics.C ...

  5. (转载)Android自定义ProgressDialog进度等待框

    Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...

  6. Android之自定义View以及画一个时钟

    https://www.2cto.com/kf/201509/443112.html 概述: 当Android自带的View满足不了开发者时,自定义View就发挥了很好的作用.建立一个自定义View, ...

  7. Android简单自定义圆形和水平ProgressBar

    ProgressBar简介 继承于View类,直接子类有AbsSeekBar和ContentLoadingProgressBar,其中AbsSeekBar的子类有SeekBar和RatingBar,可 ...

  8. Android 三档自定义滑动开关,禁止点击功能的实现,用默认的seekbar组件实现

    android三档自定义滑动开关,禁止点击功能的实现,普通开关网上有很多例子,三档滑动开关的则找了整天都没有相关例子,开始用普通开关的源码修改了自己实现了一个类,但效果不如人意,各种边界情况的算法很难 ...

  9. 接上一篇中记录Echarts进度环使用【不同状态不同进度环颜色及圈内文字】--采用单实例业务进行说明

    接上一篇中记录Echarts进度环使用 此处处理不同状态下不同进度环颜色及圈内文字等的相关处理,采用实际案例源码说明 -----------------偶是华丽丽分割线---------------- ...

随机推荐

  1. 使用 Topshelf 结合 Quartz.NET 创建 Windows 服务

    Ø  前言 之前一篇文章已经介绍了,如何使用 Topshelf 创建 Windows 服务.当时提到还缺少一个任务调度框架,就是 Quartz.NET.而本文就展开对 Quartz.NET 的研究,以 ...

  2. Nginx正反向代理、负载均衡等功能实现配置

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   系统环境: VirtualBox Manager Centos6.4 nginx1.10.0 IP对应的机器名: IP ...

  3. [C++]PAT乙级1012.数字分类 (20/20)

    /* 1012. 数字分类 (20) 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和, ...

  4. ue4 蓝图方法备份

    normalized 标准化 向量标准化 +- 1,1,1 内的值 角度标准化 +-180内的值 delta A-B (输出时roll在后面)  角度相减 interp 插值运算 (做平滑移动常用)  ...

  5. Spark思维导图之Spark SQL

  6. 360doc个人图书馆解决复制问题

    360doc个人图书馆在复制的时候会弹出如下页面: 对于我们程序员来说很容易就可以推断,可能是在复制的时候写了事件什么的. 估计是这些个: document.oncopy或者document.body ...

  7. iframe标签

    转载文章:Web前端之iframe详解 iframe基本内涵 通常我们使用iframe直接在页面嵌套iframe标签指定src就可以了. <iframe src="demo_ifram ...

  8. python 数据分析2

    本节概要 Numpy详解 安装 Numpy的安装已经不想多说..在确保pip或pip3的路径被添加到系统环境变量里面之后,就可以直接用下面语句进行安装. pip install numpy or pi ...

  9. 自定义Banner

    Spring Boot项目启动时,默认的打印样式如下 自定义 在/src/main/resources目录下新建banner.txt,在里面输入要打印的文字即可,例如: 图形制作网站:http://w ...

  10. You Only Look Once: Unified, Real-Time Object Detection(翻译)

    0 - 摘要 我们提出了YOLO,一种新的物体检测方法.之前的物体检测工作是通过重新使用分类器来进行检测.相反,我们将对象检测抽象为一个回归问题,描述为以空间分隔的边界框和相关的类别概率.一个简单的神 ...