Android一个自定义的进度环:ProgressChart

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

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

主要代码如下:

public class ProgressChart extends View
{private Context context;//圆环背景画笔private Paint paintBg;//圆环进度画笔private Paint paintProgress;//文字画笔1private Paint paintText1;//文字画笔2private Paint paintText2;// 圆环的宽度 private float progressWidth;  //圆环的区域private RectF roundRect;//文字的区域private Rect textRect;//单个字符的区域private Rect charRect;//绘制时每次增加的度数private float rotateDegree = 1.0F;//绘制开始的度数private float startDegree = 0.0F;//结束的度数private float endDegree;//背景颜色private int bgColor;//进度颜色private int progressColor;// 中间进度百分比的字符串的颜色 private int textColor;  //字符串的文字大小private float text1Size;//字符串的文字大小private float text2Size;//绘制的字符串private String text="0%";public ProgressChart(Context context) {this(context, null);}public ProgressChart(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ProgressChart(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);this.context=context;init(attrs);}private void init(AttributeSet attrs) {TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.ProgressChart);this.progressWidth = mTypedArray.getDimension(R.styleable.ProgressChart_progressWidth, 8);this.text1Size = mTypedArray.getDimension(R.styleable.ProgressChart_textSize1, 32);this.text2Size = mTypedArray.getDimension(R.styleable.ProgressChart_textSize2, 20);this.bgColor = mTypedArray.getColor(R.styleable.ProgressChart_bgColor,Color.parseColor("#fff2f2f2"));this.progressColor = mTypedArray.getColor(R.styleable.ProgressChart_progressColor,Color.parseColor("#fffd0000"));this.textColor = mTypedArray.getColor(R.styleable.ProgressChart_txtColor,Color.parseColor("#fffd0000"));mTypedArray.recycle();this.textRect = new Rect();this.charRect = new Rect();this.paintBg = new Paint();this.paintBg.setStyle(Paint.Style.STROKE);this.paintBg.setStrokeWidth(this.progressWidth);this.paintBg.setColor(this.bgColor);this.paintProgress = new Paint();this.paintProgress.setStyle(Paint.Style.STROKE);this.paintProgress.setStrokeWidth(this.progressWidth);this.paintProgress.setColor(this.progressColor);this.paintText1 = new Paint();this.paintText1.setTextSize(this.text1Size);this.paintText1.setTextAlign(Paint.Align.CENTER);this.paintText1.setColor(this.textColor);this.paintText2 = new Paint();this.paintText2.setTextSize(this.text2Size);this.paintText2.setTextAlign(Paint.Align.CENTER);this.paintText2.setColor(this.textColor);}public boolean setProgress(String progress){this.text = DecimalFormat.getPercentInstance().format(Double.valueOf(progress));this.startDegree = 0.0F;this.endDegree = (360.0F * Float.valueOf(progress).floatValue());this.rotateDegree = (this.endDegree / 40.0F);invalidate();return true;}@Overrideprotected void onDraw(Canvas canvas){//绘制圆环背景canvas.drawArc(this.roundRect, 0.0F, 360.0F, false, this.paintBg);//绘制进度if (this.startDegree < this.endDegree){canvas.drawArc(this.roundRect, -90.0F, this.startDegree, false, this.paintProgress);this.startDegree += this.rotateDegree;invalidate();}else{canvas.drawArc(this.roundRect, -90.0F, this.endDegree, false, this.paintProgress);}if(!TextUtils.isEmpty(this.text)){//绘制文字this.paintText1.getTextBounds(this.text, 0, this.text.length(), this.textRect);this.paintText2.getTextBounds("%", 0, 1, this.charRect);FontMetricsInt fontMetricsInt = this.paintText1.getFontMetricsInt();float y = this.roundRect.top + (this.roundRect.bottom - this.roundRect.top - fontMetricsInt.bottom + fontMetricsInt.top) / 2.0F - 5 * fontMetricsInt.top / 5;canvas.drawText(this.text.replace("%", ""), this.roundRect.centerX() - this.charRect.width() / 2, y, this.paintText1);canvas.drawText("%", this.roundRect.centerX() + this.textRect.width() / 2 - this.charRect.width() / 2, y, this.paintText2);}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {this.roundRect = new RectF(this.progressWidth, this.progressWidth, w - this.progressWidth, h - this.progressWidth);}}

 

转载于:https://www.cnblogs.com/rainboy2010/p/5391643.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/418659.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1876)vue之电商管理系统电商系统之整体布局

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

0414-复利计算再升级

目录 项目简介更新内容设计概要估算工作量任务分配github地址演示总结一、项目简介 项目名复利计算5.1版本号5.1开发语言java开发工具eclipse、IDEA、mysql更新内容功能完善开发人员颜文生、林集团二、更新内容 界面美化 改善交互 三、概要设计 加载图片美化界面&#xff0c;利…

前端学习(1874)vue之电商管理系统电商系统之修改element-ui组件的按需导入

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

前端学习(1877)vue之电商管理系统电商系统之头部布局

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

前端学习(1878)vue之电商管理系统电商系统之左侧菜单布局

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

前端学习(1879)vue之电商管理系统电商系统之通过axios拦截器添加token认证

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

前端学习(1880)vue之电商管理系统电商系统之获取左侧菜单数据

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

OpenGL学习之路(二)

1 引子 在上一篇读书笔记中&#xff0c;我们对书本中给出的例子进行详细的分析。首先是搭出一个框架&#xff1b;然后填充初始化函数&#xff0c;在初始化函数中向OpenGL提供顶点信息&#xff08;缓冲区对象&#xff09;和顶点属性信息&#xff08;顶点数组对象&#xff09;&a…

前端学习(1881)vue之电商管理系统电商系统之双层for循环渲染数据

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

iOS之本地推送通知使用介绍

推送通知作用&#xff1a;可以让不在前台运行的app,告知用户app内部发生了什么事情 如何发送本地通知&#xff1a; 发送本地通知步骤&#xff1a; 1.创建本地通知对象 2.设置本地通知内容 3.调度本地通知 具体实现代码&#xff1a; 注意&#xff1a;iOS7通过上述代码可以完成发…

前端学习(1882)vue之电商管理系统电商系统之设置字体颜色并添加标签

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

前端学习(1883)vue之电商管理系统电商系统之每次只能打开一个菜单项并解决边框问题

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

第一次冲刺阶段(三)

今天下午我们团队进行了第三次站立会议&#xff0c;总结了昨天的进展状况&#xff0c;的的确确出现很多问题&#xff0c;进展微乎其微&#xff0c;我们对安卓开发知识不熟悉&#xff0c;导致我们举步维艰&#xff0c;很考验耐性与团队的协作能力&#xff0c;我们必须坚持下去&a…

前端学习(1884)vue之电商管理系统电商系统之实现侧边栏的折叠和展开

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vueVue.use(Router)const router new Router({routes:[{path:/,redirect:/login},{path:/login,component:Login},{pat…

前端学习(1885)vue之电商管理系统电商系统之首页路由的重定向

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

前端学习(1886)vue之电商管理系统电商系统之首页路由的重定向主页侧边栏路由链接的改造

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

Android adb logcat使用技巧

前言 新买的笔记本E431装了最新版的Eclipse&#xff0c;搞定了Android开发环境&#xff0c;可是logcat里查看东西居然仅仅显示level&#xff0c;没有错误的具体信息。我本身也不是一个愿意折腾图形界面&#xff0c;更喜欢纯命令行的操作&#xff0c;因此今天在明昊师兄的建议下…

前端学习(1887)vue之电商管理系统电商系统之通过路由的形式显示用户列表

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…

前端学习(1888)vue之电商管理系统电商系统之在sessionStrorage中保存按钮的激活状态

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue Vue.use(Router)const router new Router({routes: [{path: /,re…