自定义圆环progressbar-程序员宅基地

技术标签: java  ui  移动开发  

RoundProgressBar.java

  1. /**
  2. * RoundProgressBar.java [v1.0.0]
  3. * classes: com.example.audiorecordingtest.RoundProgressBar
  4. * Amanda Create at 2014年10月10日 下午4:16:45
  5. * Copyright 阳光健康信息技术有限公司
  6. */
  7. package com.example.audiorecordingtest.view;
  8. /**
  9. * com.example.audiorecordingtest.RoundProgressBar
  10. * @author Amanda
  11. * create at 2014年10月10日 下午4:16:45
  12. */
  13. import com.example.audiorecordingtest.R;
  14. import com.example.audiorecordingtest.R.styleable;
  15. import android.content.Context;
  16. import android.content.res.TypedArray;
  17. import android.graphics.Canvas;
  18. import android.graphics.Color;
  19. import android.graphics.Paint;
  20. import android.graphics.RectF;
  21. import android.graphics.Typeface;
  22. import android.util.AttributeSet;
  23. import android.view.View;
  24. /**
  25. * 仿iphone带进度的进度条,线程安全的View,可直接在线程中更新进度
  26. *
  27. */
  28. public class RoundProgressBar extends View {
  29. /**
  30. * 画笔对象的引用
  31. */
  32. private Paint paint;
  33. /**
  34. * 圆环的颜色
  35. */
  36. private int roundColor;
  37. /**
  38. * 圆环进度的颜色
  39. */
  40. private int roundProgressColor;
  41. /**
  42. * 中间进度百分比的字符串的颜色
  43. */
  44. private int textColor;
  45. /**
  46. * 中间进度百分比的字符串的字体
  47. */
  48. private float textSize;
  49. /**
  50. * 圆环的宽度
  51. */
  52. private float roundWidth;
  53. /**
  54. * 最大进度
  55. */
  56. private int max;
  57. /**
  58. * 当前进度
  59. */
  60. private int progress;
  61. /**
  62. * 是否显示中间的进度
  63. */
  64. private boolean textIsDisplayable;
  65. /**
  66. * 进度的风格,实心或者空心
  67. */
  68. private int style;
  69. public static final int STROKE = 0;
  70. public static final int FILL = 1;
  71. public RoundProgressBar(Context context) {
  72. this(context, null);
  73. }
  74. public RoundProgressBar(Context context, AttributeSet attrs) {
  75. this(context, attrs, 0);
  76. }
  77. public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
  78. super(context, attrs, defStyle);
  79. paint = new Paint();
  80. TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
  81. R.styleable.RoundProgressBar);
  82. //获取自定义属性和默认值
  83. roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.rgb(245, 252, 248));
  84. roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.rgb(62, 187, 102));
  85. textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLACK);
  86. textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
  87. roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
  88. max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
  89. textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
  90. style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
  91. mTypedArray.recycle();
  92. }
  93. @Override
  94. protected void onDraw(Canvas canvas) {
  95. super.onDraw(canvas);
  96. /**
  97. * 画最外层的大圆环
  98. */
  99. int centerX = getWidth()/2;
  100. int centerY = getHeight()/2;
  101. int centre = centerX>centerY?centerY:centerX;
  102. int radius = (int) (centre - roundWidth/2); //圆环的半径
  103. paint.setColor(roundColor); //设置圆环的颜色
  104. paint.setStyle(Paint.Style.STROKE); //设置空心
  105. paint.setStrokeWidth(roundWidth); //设置圆环的宽度
  106. paint.setAntiAlias(true); //消除锯齿
  107. canvas.drawCircle(centerX, centerY, radius, paint); //画出圆环
  108. // Log.e("log", centre + "");
  109. /**
  110. * 画进度百分比
  111. */
  112. paint.setStrokeWidth(0);
  113. paint.setColor(textColor);
  114. paint.setTextSize(textSize);
  115. paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体
  116. int percent = (int)(((float)progress / (float)max) * 100); //中间的进度百分比,先转换成float在进行除法运算,不然都为0
  117. float textWidth = paint.measureText(percent + "%"); //测量字体宽度,我们需要根据字体的宽度设置在圆环中间
  118. if(textIsDisplayable && percent != 0 && style == STROKE){
  119. canvas.drawText(percent + "%", centerX - textWidth / 2, centerY + textSize/2, paint); //画出进度百分比
  120. }
  121. /**
  122. * 画圆弧 ,画圆环的进度
  123. */
  124. //设置进度是实心还是空心
  125. paint.setStrokeWidth(roundWidth); //设置圆环的宽度
  126. paint.setColor(roundProgressColor); //设置进度的颜色
  127. RectF oval = new RectF(centerX - radius, centerY - radius, centerX
  128. + radius, centerY + radius); //用于定义的圆弧的形状和大小的界限
  129. switch (style) {
  130. case STROKE:{
  131. paint.setStyle(Paint.Style.STROKE);
  132. canvas.drawArc(oval, 90, 360 * progress / max, false, paint); //根据进度画圆弧
  133. break;
  134. }
  135. case FILL:{
  136. paint.setStyle(Paint.Style.FILL_AND_STROKE);
  137. if(progress !=0){
  138. canvas.drawArc(oval, 90, 360 * progress / max, true, paint); //根据进度画圆弧
  139. }
  140. break;
  141. }
  142. }
  143. }
  144. public synchronized int getMax() {
  145. return max;
  146. }
  147. /**
  148. * 设置进度的最大值
  149. * @param max
  150. */
  151. public synchronized void setMax(int max) {
  152. if(max < 0){
  153. throw new IllegalArgumentException("max not less than 0");
  154. }
  155. this.max = max;
  156. }
  157. /**
  158. * 获取进度.需要同步
  159. * @return
  160. */
  161. public synchronized int getProgress() {
  162. return progress;
  163. }
  164. /**
  165. * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
  166. * 刷新界面调用postInvalidate()能在非UI线程刷新
  167. * @param progress
  168. */
  169. public synchronized void setProgress(int progress) {
  170. if(progress < 0){
  171. throw new IllegalArgumentException("progress not less than 0");
  172. }
  173. if(progress > max){
  174. progress = max;
  175. }
  176. if(progress <= max){
  177. this.progress = progress;
  178. postInvalidate();
  179. }
  180. }
  181. public int getCricleColor() {
  182. return roundColor;
  183. }
  184. public void setCricleColor(int cricleColor) {
  185. this.roundColor = cricleColor;
  186. }
  187. public int getCricleProgressColor() {
  188. return roundProgressColor;
  189. }
  190. public void setCricleProgressColor(int cricleProgressColor) {
  191. this.roundProgressColor = cricleProgressColor;
  192. }
  193. public int getTextColor() {
  194. return textColor;
  195. }
  196. public void setTextColor(int textColor) {
  197. this.textColor = textColor;
  198. }
  199. public float getTextSize() {
  200. return textSize;
  201. }
  202. public void setTextSize(float textSize) {
  203. this.textSize = textSize;
  204. }
  205. public float getRoundWidth() {
  206. return roundWidth;
  207. }
  208. public void setRoundWidth(float roundWidth) {
  209. this.roundWidth = roundWidth;
  210. }
  211. }

values/attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="RoundProgressBar">
  4. <attr name="roundColor" format="color"/>
  5. <attr name="roundProgressColor" format="color"/>
  6. <attr name="roundWidth" format="dimension"></attr>
  7. <attr name="textColor" format="color" />
  8. <attr name="textSize" format="dimension" />
  9. <attr name="max" format="integer"></attr>
  10. <attr name="textIsDisplayable" format="boolean"></attr>
  11. <attr name="style">
  12. <enum name="STROKE" value="0"></enum>
  13. <enum name="FILL" value="1"></enum>
  14. </attr>
  15. </declare-styleable>
  16. </resources>



layout/audiorecord.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:android_custom="http://schemas.android.com/apk/res/com.example.audiorecordingtest"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:gravity="center">
  8. <FrameLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="0dp"
  11. android:layout_weight="2">
  12. <ImageView
  13. android:id="@+id/img_microfan"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center"
  17. android:src="@drawable/microfan"
  18. android:scaleType="centerInside"/>
  19. <com.example.audiorecordingtest.view.RoundProgressBar
  20. android:id="@+id/roundProgressBar"
  21. android:layout_width="match_parent"
  22. android:layout_height="match_parent"
  23. android_custom:roundColor="#f6fcf8"
  24. android_custom:roundProgressColor="#41c36b"
  25. android_custom:textIsDisplayable="false"
  26. android_custom:roundWidth="5dp"/>
  27. </FrameLayout>
  28. <TextView
  29. android:id="@+id/txt_microfan"
  30. android:layout_width="match_parent"
  31. android:layout_height="0dp"
  32. android:layout_weight="1"
  33. android:gravity="center"
  34. android:text="正在录音……"
  35. android:textColor="#3c3c3c"
  36. android:textSize="30px"/>
  37. </LinearLayout>





版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_33670786/article/details/85790932

智能推荐

go闭包递归-程序员宅基地

文章浏览阅读137次。因为a()执行完后,b()没有被返回给a()的外界,只是被a()所引用,而此时a()也只会被b()引 用,因此函数a()和b()互相引用但又不被外界打扰(被外界引用),函数a和b就会被GC回收。所以直接调用a();在上面的例子中,由于闭包的存在使得函数a()返回后,a中的i始终存在,这样每次执行c(),i都是自加1后的值。从上面可以看出闭包的作用就是在a()执行完并返回后,闭包使得Javascript的垃圾回收机制GC不会收回a()所占用的资源,因为a()的内部函数b()的执行需要依赖a()中的变量i。

abcde / fghij = n_a-j代表0-9abcde/fghij=x-程序员宅基地

文章浏览阅读1.9k次。问题:输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列,2&lt;=n&lt;=79。样例输入: 62样例输出: 79546/01238=62 94736/01528=62解:public class Main { static int[] a = new int[10]; public ..._a-j代表0-9abcde/fghij=x

关于Fuzz——peach的学习-程序员宅基地

文章浏览阅读769次。  最近在搞漏洞挖掘,之前写过一个文件格式漏洞挖掘的博文,使用的是光刃牛写的Alpha Fuzz工具。感觉样本生成的质量不是很好,这次打算使用一下老牌的Fuzz工具peach。学长介绍了一下说peach的样本生成算法比较屌,质量比较高。所以这次来学习一下。  另外最近打算搞android方面的漏洞挖掘和分析,博客也没怎么写。这次也是想学习一下android平台的漏洞挖掘,感觉andr..._tcp peach fuzz

Joint Deep Learning For Pedestrian Detection(论文笔记-深度学习:行人检测)-程序员宅基地

文章浏览阅读6.9k次。一、摘要: 行人检测主要分为四部分:特征提取、形变处理、遮挡处理和分类。现存方法都是四个部分独立进行,本文联合深度学习将四个部分结合在一起,最大化其能力。 二、引言: (1)首先,特征提取的应该是行人最有判别力的特征,比较有名的特征描述子有:Haar-like、SIFT、HOG等等; (2)其次,可变形模型应该可以处理人体的各个部分,如:躯干、头、退_joint deep learning for pedestrian detection

欧姆龙模拟量模块ad041_欧姆龙PLC模拟量AD模块使用方法-程序员宅基地

文章浏览阅读1.2w次,点赞2次,收藏26次。1,选择合适的AD模块,以下以CPM1A-AD041模块为例:2,确定AD041模块量程,等参数:此次选择量程选择01:0到10v 平均值功能 0:不使用 模拟输入其地位 1:启动通道参数为:1001(二进制)--->#9(十六进制)3,确定AD041模块输入输出通道根据实际情况此次选择输入输出通道: AD041参数通道#1,#2:1001通道,101通道:低4位d0--d3存储#1通道参数..._欧姆龙ad041模块说明书

oracle case语句效率高,oracle的case 提交执行效率-程序员宅基地

文章浏览阅读682次。--sql identityCREATE TABLE dbo.Customer(customerid INT IDENTITY PRIMARY KEY,firstname VARCHAR(40) NOT NULL,lastname VARCHAR(40) NOT NULL,statecode VARCHAR(2) NOT NULL,totalsales money NOT NULL DEFAULT..._orace case影响效率吗

随便推点

pyqt5 实现多窗口跳转的方法,以及ui界面代码与逻辑代码分离_pyqt5 designer多页面切换-程序员宅基地

文章浏览阅读373次,点赞3次,收藏7次。【代码】pyqt5 实现多窗口跳转的方法,以及ui界面代码与逻辑代码分离。_pyqt5 designer多页面切换

2021-2025年中国三维飞行时间图像传感器行业市场供需与战略研究报告_heptagon公司简介-程序员宅基地

文章浏览阅读76次。三维飞行时间图像传感器市场的企业竞争态势 该报告涉及的主要国际市场参与者有Texas Instruments、STMicroelectronics、PMD Technologies、Infineon、PrimeSense (Apple)、MESA (Heptagon)、Melexis、ifm Electronic、Canesta (Microsoft)、Espros Photonics、TriDiCam等。这些参与者的市场份额、收入、公司概况和SWOT分析都包含在三维飞行时间图像传感器市..._heptagon公司简介

jQuey/js 省市县三级下拉框联动的回显(简单易懂)_jquery 省市区三级联动回显-程序员宅基地

文章浏览阅读4.7k次,点赞5次,收藏13次。简单易懂的多级菜单联动实现省市县功能并且支持回显_jquery 省市区三级联动回显

为什么几乎所有的量化交易都用Python?-程序员宅基地

文章浏览阅读728次,点赞10次,收藏12次。因为python好用啊!Python,作为一种功能强大且易于学习的编程语言,已经成为的首选工具。以下我将从3个方面给大家分析python为何成为的理想选择。Python就像我们的日常语言一样简单明了。想象一下,你在写一个故事而不是复杂的代码。这使得即使是编程新手也能轻松上手。由于Python的简单性,出错的机会大大减少。这意味着金融分析师可以将更多的时间和精力放在真正的分析工作上,而不是纠结于编程中的小问题。无论你手头上的数据是什么样子,Python都能帮你轻松处理。

追溯XLNet的前世今生:从Transformer到XLNet-程序员宅基地

文章浏览阅读3.4k次,点赞2次,收藏13次。作者丨李格映来源 |转载自程序员宅基地导读:2019 年 6 月,CMU 与谷歌大脑提出全新 XLNet,基于 BERT 的优缺点,XLNet 提出一种泛化自回归预训练..._xlnet模型是基于什么架构的

java log4j设置-程序员宅基地

文章浏览阅读44次。文件:log4j.propertieslog4j.rootLogger=DEBUG,R log4j.appender.R=org.apache.log4j.FileAppender log4j.appender.R.file=my.log log4j.appender.R.Append=true log4j.appender.R.layout=org.ap...