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

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

主要代码如下:

public class ProgressChart extends View
{ private Context context; //圆环背景画笔
private Paint paintBg; //圆环进度画笔
private Paint paintProgress; //文字画笔1
private Paint paintText1; //文字画笔2
private 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;
} @Override
protected 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);
}
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
this.roundRect = new RectF(this.progressWidth, this.progressWidth, w - this.progressWidth, h - this.progressWidth);
} }

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. SQL Server进阶(十一)存储过程

    存储过程和函数的区别 存储过程是第一次编译之后就会被存储的下来的预编译对象,之后无论何时调用它都会去执行已经编译好的代码.而函数每次执行都需要编译一次.总结下来有下面几个区别: 基本不同: 函数必须有 ...

  2. ASP.NET MVC利用PagedList分页(一)

    前几天看见博客园上有人写ASP.NET MVC的分页思想,这让我不禁想起了PagedList.PagedList是NuGet上提供的一个分页的类库,能对任何IEnumerable<T>进行 ...

  3. group by having 判断重复的有几条数据

    判断字段id和字段zhi重复的条数 group by 和having 解释:前提必须了解sql语言中一种特殊的函数:聚合函数,--例如SUM, COUNT, MAX, AVG等.这些函数和其它函数的根 ...

  4. Python Django CBV下的通用视图函数

    ListView TemplateView DetailView 之前的代码实例基本上都是基于FBV的模式来撰写的,好处么,当然就是简单粗暴..正如: def index(request): retu ...

  5. npm 常用命令详解(转载)

    学习gulp的使用时,对npm的掌握是必不可少的,经常到npm官网查询文档让我感到不爽,还不如整理了一些常用的命令博客上,于是根据自己的理解简单翻译过来,终于有点输出,想学习npm这块的朋友不可错过这 ...

  6. Flask中Mysql数据库的常见操作

    from flask import Flask,render_template #导入第三方链接库sql点金术 from flask_sqlalchemy import SQLAlchemy #建立对 ...

  7. jsp映射为其他地址上去 ???

    在web.xml 里面配置servlet不起作用,所以配置jsp,然后在jsp里面跳转到servlet去   ???  第八天中的<08-jsp常用标签.avi> 在web.xml里面先设 ...

  8. 操作dom影响性能的原因

    为什么dom操作会影响性能? 在浏览器当中,dom的实现和ECMAScript的实现是分离的. 例如,在IE中,ECMAScrit的实现在jscript.dll中,而DOM的实现在mshtml.dll ...

  9. neutron-----openstack网络操作

    #查看子网 openstack subnet list #删除子网 openstack subnet delete name/id #查看网络 openstack network list #删除网络 ...

  10. python 指定日期图片文件删除

    手机使用python操作图片文件 起因 前几天去国图拍了一本书,一本心理学方面的书,也许你问我为什么不去买一本,或者去网上找pdf. 其实吧,关于心理学方面的书可以说在市面上一抓就是一堆,至于拍这本书 ...