Android进度条学习

自定义属性
<!--
roundColor 圆环的颜色 roundProgressColor 进度的颜色 roundWidth 圆环的宽度 textColor 文字颜色 textSize 文字大小 max 最大值 textIsDisplayable 是否显示进度文本 style 样式
STROKE 空心
FILL 实心
--> <declare-styleable name="RoundProgressBar">
<attr name="roundColor" format="color"/>
<attr name="roundProgressColor" format="color"/>
<attr name="roundWidth" format="dimension"></attr>
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="max" format="integer"></attr>
<attr name="textIsDisplayable" format="boolean"></attr>
<attr name="style">
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr>
</declare-styleable>
public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
/**
* 获取自定义的属性
*/
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
//底色
mRoundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
//进度的颜色
mRoundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.BLUE);
//圆形的宽
mRoundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 20);
//字体颜色 中间
mTextColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLUE);
//中间进度显示的字体大小
mTextSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
//最大值
mMax = typedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
//文字是否显示
mTextIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
//实心或者 空心
mStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, 0);
typedArray.recycle();
}
绘制
//圆心
int centerOfCircle = getWidth() / 2;
//radius 半径
int radius = (int) (centerOfCircle - mRoundWidth / 2); //设置画笔
mPaint.setAntiAlias(true);
//圆环的颜色
mPaint.setColor(mRoundColor); //设置空心
mPaint.setStyle(Paint.Style.STROKE); //画笔宽度
mPaint.setStrokeWidth(mRoundWidth); //画圆
canvas.drawCircle(centerOfCircle, centerOfCircle, radius, mPaint); /**
* 画百分比
*/
mPaint.setStrokeWidth(0);
//字体大小
mPaint.setTextSize(mTextSize);
//画笔颜色
mPaint.setColor(mTextColor);
//字体
mPaint.setTypeface(Typeface.DEFAULT_BOLD); //计算百分比
int percent = (int) (((float) mProgress / (float) mMax) * 100); //测量字体的宽度
float textWidth = mPaint.measureText(percent + "%");
//判断是否显示进度文字 不是0,风格是空心的
if (mTextIsDisplayable && percent != 0 && mStyle == STROKE) { canvas.drawText(percent + "%", centerOfCircle - textWidth / 2, centerOfCircle + textWidth / 2, mPaint);
} /**
* 设置进度
*/
mPaint.setColor(mRoundProgressColor);
//画笔宽度
mPaint.setStrokeWidth(mRoundWidth);
mPaint.setAntiAlias(true); RectF oval = new RectF(centerOfCircle - radius, centerOfCircle - radius, centerOfCircle + radius, centerOfCircle + radius); switch (mStyle) { case STROKE:
//空心
mPaint.setStyle(Paint.Style.STROKE);
//画圆弧 /**
*
*开始的角度
*/
canvas.drawArc(oval, 180, 360 * mProgress / mMax, false, mPaint);
break;
case FILL:
//实心
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//画圆弧
if(mProgress!=0) {
canvas.drawArc(oval, 180, 360 * mProgress / mMax, true, mPaint);
}
break;
}
源码:
https://github.com/ln0491/ProgressDemo
Android进度条学习的更多相关文章
- 多种的android进度条的特效源码
多种的android进度条的特效源码,这个源码是在源码天堂那个网站上转载过来的,我已经修改一部分了,感觉很实用的,大家可以学习一下吧,我就不上传源码了,大家可以直接到那个网站上下载吧. 源码天堂下载地 ...
- android进度条
android进度条 1.达到的效果 2.布局代码 先写一个my_browser.xml文件 存放WebView <?xml version="1.0" encoding=& ...
- iOS之小功能模块--彩虹动画进度条学习和自主封装改进
前言: 首先展示一下这个iOS小示例的彩色进度条动画效果: 阅读本文先说说好处:对于基础不好的读者,可以直接阅读文末尾的"如何使用彩虹动画进度条"章节,然后将我封装好的这个功能模块 ...
- Android 进度条改变图片透明度
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android 进度条
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android—进度条
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Android——进度条ProgressBar
1.activity_progressbar.xml <?xml version="1.0" encoding="utf-8"?><Linea ...
- android进度条的使用
// 导入按钮事件 btnsearch.setOnClickListener(new View.OnClickListener() { @Override public void on ...
- Android 进度条按钮实现(ProgressButton)
有些App在点击下载按钮的时候,可以在按钮上显示进度,我们可以通过继承原生Button,重写onDraw来实现带进度条的按钮. Github:https://github.com/imcloudflo ...
随机推荐
- 在ASP.NET Web API项目中使用Hangfire实现后台任务处理
当前项目中有这样一个需求:由前端用户的一个操作,需要触发到不同设备的消息推送.由于推送这个具体功能,我们采用了第三方的服务.而这个服务调用有时候可能会有延时,为此,我们希望将消息推送与用户前端操作实现 ...
- Objective-C中的内存管理
在编程语言中是少不了对内存的管理的,内存对于计算机来说是宝贵的资源,所以对使用不到的资源进行回收是很有必要的.OC中使用引用计数和垃圾回收来管理内存,在OC中为每个对象分配一个引用计数器,当对象刚刚被 ...
- 1Z0-053 争议题目解析134
1Z0-053 争议题目解析134 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 134.You are managing an Oracle Database 11g datab ...
- 1Z0-053 争议题目解析606
1Z0-053 争议题目解析606 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 606.Identify the channel settings that can be per ...
- 记录一则RMAN备份策略修正案例
背景:在给某客户处理问题时,发现客户数据库的备份空间即将用尽,进一步查看发现是用户数据库的当前RMAN备份策略存在潜在问题,需要修改备份策略. 环境:SunOS 5.10 + Oracle 11.2. ...
- 重温Servlet学习笔记--request对象
request和response是一对搭档,一个负责请求一个负责响应,都是Servlet.service()方法的参数,response的知识点前面梳理过了,这里只说一下request,在客户端发出每 ...
- (一)FlexViewer之整体框架解析
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.FlexViewer简介 FlexViewer框架为Esri提供的 ...
- Node.js连接Mysql,并把连接集成进Express中间件中
引言 在node.js连接mysql的过程,我们通常有两种连接方法,普通连接和连接池. 这两种方法较为常见,当我们使用express框架时还会选择使用中间express-myconnection,可以 ...
- Python_Day_02 str内部方法总结
刚开始学习Python,看了一天的字符串内部方法,现在来总结一下. capitalize(self) 将一句话的首字母变大写,其他字母都变小 name = "love PyThon" ...
- mysql 日期函数总结
1.0 格式化:DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) date 参数是合法的日期.format 规定日期/时间的 ...