1. package com.timeshare.tmband.Utils;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.RectF;
  9. import android.graphics.Typeface;
  10. import android.util.AttributeSet;
  11. import android.util.Log;
  12. import android.view.View;
  13.  
  14. import com.timeshare.tmband.R;
  15.  
  16. /**
  17. * 仿iphone带进度的进度条,线程安全的View,可直接在线程中更新进度
  18. *
  19. * @author ailin
  20. *
  21. */
  22. public class RoundProgressBar extends View {
  23. /**
  24. * 画笔对象的引用
  25. */
  26. private Paint paint;
  27.  
  28. /**
  29. * 圆环的颜色
  30. */
  31. private int roundColor;
  32.  
  33. /**
  34. * 圆环进度的颜色
  35. */
  36. private int roundProgressColor;
  37.  
  38. /**
  39. * 中间进度百分比的字符串的颜色
  40. */
  41. private int textColor;
  42.  
  43. /**
  44. * 中间进度百分比的字符串的字体
  45. */
  46. private float textSize;
  47.  
  48. /**
  49. * 圆环的宽度
  50. */
  51. private float roundWidth;
  52.  
  53. /**
  54. * 最大进度
  55. */
  56. private int max;
  57.  
  58. /**
  59. * 当前进度
  60. */
  61. private int progress;
  62.  
  63. private int hour=0;
  64. private int min=0;
  65. private String tear=null;
  66.  
  67. /**
  68. * 是否显示中间的进度
  69. */
  70. private boolean textIsDisplayable;
  71.  
  72. /**
  73. * 进度的风格,实心或者空心
  74. */
  75. private int style;
  76.  
  77. public static final int STROKE = 0;
  78. public static final int FILL = 1;
  79.  
  80. public RoundProgressBar(Context context) {
  81. this(context, null); //在java代码创建视图的时候被调用,如果是从xml填充的视图,就不会调用这个
  82. }
  83.  
  84. public RoundProgressBar(Context context, AttributeSet attrs) {
  85. this(context, attrs, 0);
         //这个是在xml创建但是没有指定style的时候被调用
  86. }


  87. public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
  88. super(context, attrs, defStyle);
  89.  
  90. paint = new Paint();
  91.  
  92. TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
  93. R.styleable.RoundProgressBar);
  94.  
  95. // 获取自定义属性和默认值
  96. roundColor = mTypedArray.getColor(
  97. R.styleable.RoundProgressBar_roundColor, Color.RED);
  98. roundProgressColor = mTypedArray.getColor(
  99. R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
  100. textColor = mTypedArray.getColor(
  101. R.styleable.RoundProgressBar_textColor, Color.GREEN);
  102. textSize = mTypedArray.getDimension(
  103. R.styleable.RoundProgressBar_textSize, 15);
  104. roundWidth = mTypedArray.getDimension(
  105. R.styleable.RoundProgressBar_roundWidth, 5);
  106. max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
  107. textIsDisplayable = mTypedArray.getBoolean(
  108. R.styleable.RoundProgressBar_textIsDisplayable, true);
  109. style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
  110.  
  111. mTypedArray.recycle();
  112. }
  113.  
  114. @Override
  115. protected void onDraw(Canvas canvas) {
  116. super.onDraw(canvas);
  117.  
  118. /**
  119. * 画最外层的大圆环
  120. */
  121. int centre = getWidth() / 2; // 获取圆心的x坐标
  122. int radius = (int) (centre - roundWidth / 2); // 圆环的半径
  123. paint.setColor(roundColor); // 设置圆环的颜色
  124. paint.setStyle(Paint.Style.STROKE); // 设置空心
  125. paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
  126. paint.setAntiAlias(true); // 消除锯齿
  127. canvas.drawCircle(centre, centre, radius, paint); // 画出圆环
  128.  
  129. Log.e("log", centre + "");
  130.  
  131. /**
  132. * 画进度百分比
  133. */
  134. paint.setStrokeWidth(0);
  135. paint.setColor(textColor);
  136. paint.setTextSize(textSize);
  137. paint.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体
  138. int percent = (int) (((float) progress / (float) max) * 100); // 中间的进度百分比,先转换成float在进行除法运算,不然都为0
  139. float textWidth = paint.measureText(percent + ""); // 测量字体宽度,我们需要根据字体的宽度设置在圆环中间
  140.  
  141. if (textIsDisplayable && style == STROKE) {
  142. canvas.drawText(percent + "", centre - textWidth / 2, centre
  143. + textSize / 2, paint); // 画出进度百分比
  144.  
  145. Paint paint1 = new Paint();
  146. paint1.setStrokeWidth(0);
  147. paint1.setColor(textColor);
  148. paint1.setTextSize(14);
  149. paint1.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体
  150. canvas.drawText("%", centre + textWidth / 2, centre + textSize / 2,
  151. paint1); // 画出进度百分比
  152. }
  153.  
  154. /**
  155. * 画圆弧 ,画圆环的进度
  156. */
  157.  
  158. // 设置进度是实心还是空心
  159. paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
  160. paint.setColor(roundProgressColor); // 设置进度的颜色
  161. RectF oval = new RectF(centre - radius, centre - radius, centre
  162. + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
  163.  
  164. switch (style) {
  165. case STROKE: {
  166. paint.setStyle(Paint.Style.STROKE);
  167. canvas.drawArc(oval, 0, 360 * progress / max, false, paint); // 根据进度画圆弧
  168. break;
  169. }
  170. case FILL: {
  171. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  172. if (progress != 0)
  173. canvas.drawArc(oval, 0, 360 * progress / max, true, paint); // 根据进度画圆弧
  174. break;
  175. }
  176. }
  177.  
  178. }
  179.  
  180. public synchronized int getMax() {
  181. return max;
  182. }
  183.  
  184. /**
  185. * 设置进度的最大值
  186. *
  187. * @param max
  188. */
  189. public synchronized void setMax(int max) {
  190. if (max < 0) {
  191. throw new IllegalArgumentException("max not less than 0");
  192. }
  193. this.max = max;
  194. }
  195.  
  196. /**
  197. * 获取进度.需要同步
  198. *
  199. * @return
  200. */
  201. public synchronized int getProgress() {
  202. return progress;
  203. }
  204.  
  205. /**
  206. * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 刷新界面调用postInvalidate()能在非UI线程刷新
  207. *
  208. * @param progress
  209. */
  210. public synchronized void setProgress(int progress) {
  211. if (progress < 0) {
  212. throw new IllegalArgumentException("progress not less than 0");
  213. }
  214. if (progress > max) {
  215. progress = max;
  216. }
  217. if (progress <= max) {
  218. this.progress = progress;
  219. postInvalidate();
  220. }
  221.  
  222. }
  223.  
  224. public int getCricleColor() {
  225. return roundColor;
  226. }
  227.  
  228. public void setCricleColor(int cricleColor) {
  229. this.roundColor = cricleColor;
  230. }
  231.  
  232. public int getCricleProgressColor() {
  233. return roundProgressColor;
  234. }
  235.  
  236. public void setCricleProgressColor(int cricleProgressColor) {
  237. this.roundProgressColor = cricleProgressColor;
  238. }
  239.  
  240. public int getTextColor() {
  241. return textColor;
  242. }
  243.  
  244. public void setTextColor(int textColor) {
  245. this.textColor = textColor;
  246. }
  247.  
  248. public float getTextSize() {
  249. return textSize;
  250. }
  251.  
  252. public void setTextSize(float textSize) {
  253. this.textSize = textSize;
  254. }
  255.  
  256. public float getRoundWidth() {
  257. return roundWidth;
  258. }
  259.  
  260. public void setRoundWidth(float roundWidth) {
  261. this.roundWidth = roundWidth;
  262. }
  263.  
  264. }

自定义view(自定义view的时候,三个构造函数各自的作用)的更多相关文章

  1. Android中自定义样式与View的构造函数中的第三个参数defStyle的意义

    零.序 一.自定义Style 二.在XML中为属性声明属性值 1. 在layout中定义属性 2. 设置Style 3. 通过Theme指定 三.在运行时获取属性值 1. View的第三个构造函数的第 ...

  2. 自定义View的三个构造函数

    自定义View有三个构造方法,它们的作用是不同的. public MyView(Context context) { super(context); } public MyView(Context c ...

  3. Java注解(自定义注解、view注入)

    注解这东西虽然在jdk1.5就加进来了,但他的存在还是因为使用Afinal框架的view注入才知道的.一直觉得注入特神奇,加了一句就可以把对应view生成了. 下面我们来认识一下注解这个东西 一.注解 ...

  4. Android 自定义View修炼-自定义弹幕效果View

    一.概述 现在有个很流行的效果就是弹幕效果,满屏幕的文字从右到左飘来飘去.看的眼花缭乱,看起来还蛮cool的 现在就是来实现这一的一个效果,大部分的都是从右向左移动漂移,本文的效果中也支持从左向右的漂 ...

  5. Collection View 自定义布局(custom flow layout)

    Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布 ...

  6. 贝塞尔曲线:原理、自定义贝塞尔曲线View、使用!!!

    一.原理 转自:http://www.2cto.com/kf/201401/275838.html Android动画学习Demo(3) 沿着贝塞尔曲线移动的Property Animation Pr ...

  7. android自定义View&自定义ViewGroup(上)

    一般自定义view需要重写的方法 void onMeasure(int widthMeasureSpec, int heightMeasureSpec) void onSizeChanged(int ...

  8. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  9. android view : 自定义

    首先,为什么要使用xml来配置view的视图,这个是mvc的一个思想,你可以把前端和数据分离,可以想一下一个及其复杂的视图假如要修改面对复杂的代码是多么的发愁,xml更明了的表达了视图.然而我们知道a ...

随机推荐

  1. 关于arm-linux-gcc的安装与配置

    在嵌入式开发中我们经常会用到arm-linux-gcc来编译我们的应用程序.作为arm-linux-gcc的入门,我们先看看如何安装arm-linux-gcc. 安装arm-linux-gcc还是比较 ...

  2. hibernate update部分更新

    hibernate update Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to ...

  3. [C++程序设计]基于对象的程序设计 基于对象的程序设计

    1. 面向对象分析(object oriented analysis,OOA)2. 面向对象设计(object oriented design,OOD)3. 面向对象编程(object oriente ...

  4. php 数组Array 删除指定键名值

    if(array_key_exists('keyname',$array)){ //检查数组中此键名是否存在: unset($array['keyname']); //删除后位置仍然保留,但清空了键名 ...

  5. Python之路第十二天,高级(5)-Python操作Mysql,SqlAlchemy

    Mysql基础 一.安装 Windows: 1.下载 http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31-winx64.zip 2.解压 ...

  6. PXE简要配置过程

    目录 1.所需服务 2.简要配置过程     1.dhcp服务     2.tftp服务     3.提供pxelinux.0配置文件     4.提供系统所需文件 1.所需服务:     dhcp服 ...

  7. django最简单表单入门

    两个html页面,存放于某个应用下的templates文件夹下. index.html 提交 点击“提交”按钮后,会调入第二个页面hello.html显示文本框的内容 原理是通过form的action ...

  8. 【转】iOS代码规范

    原文地址: http://www.cocoachina.com/ios/20150908/13335.html 简介: 本 文整理自Apple文档<Coding Guidelines for C ...

  9. Java--static interface

    http://stackoverflow.com/questions/8374646/what-is-a-static-interface-in-java http://stackoverflow.c ...

  10. MySQL库目录下db.opt文件的作用

    细心的朋友可能会发现有时候在某些库目录下有个 db.opt 文件,那这个文件是干什么用的呢?如果你用vi等编辑器打开看的话,内容很简单,是用来记录该库的默认字符集编码和字符集排序规则用的.也就是说如果 ...