仿IOS圆形下载进度条
/**
* Created by C058 on 2016/5/25.
*/
public class MyHoriztalProgressBar extends ProgressBar { private static final int DEFAULT_REACH_COLOR = 0xff24F569;
private static final int DEFAULT_UNREACH_COLOR = 0xffC0C0C0;
private static final int DEFAULT_REACH_HEIGHT = 1;
private static final int DEFAULT_UNREACH_HEIGHT = 2;
private static final int DEFAULT_TEXT_COLOR = DEFAULT_REACH_COLOR;
private static final int DEFAULT_TEXT_SIZE = 12;
private static final int DEFAULT_TEXT_OFFSET = 5; protected int mReachColor = DEFAULT_REACH_COLOR;
protected int mUnReachColor = DEFAULT_UNREACH_COLOR;
protected int mReachHeight = dp2px(DEFAULT_REACH_HEIGHT);
protected int mUnReachHeight = dp2px(DEFAULT_UNREACH_HEIGHT);
protected int mTextColor = DEFAULT_TEXT_COLOR;
protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
protected int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET); protected Paint mPaint = new Paint();
private int mRealWidth; public MyHoriztalProgressBar(Context context) {
this(context, null);
} public MyHoriztalProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public MyHoriztalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyHoriztalProgressBar);
mReachColor = ta.getColor(R.styleable.MyHoriztalProgressBar_progressbar_reach_color, mReachColor);
mUnReachColor = ta.getColor(R.styleable.MyHoriztalProgressBar_progressbar_unreach_color, mUnReachColor);
mReachHeight = (int) ta.getDimension(R.styleable.MyHoriztalProgressBar_progressbar_reach_height, mReachHeight);
mUnReachHeight = (int) ta.getDimension(R.styleable.MyHoriztalProgressBar_progressbar_unreach_height, mUnReachHeight);
mTextColor = ta.getColor(R.styleable.MyHoriztalProgressBar_progressbar_text_color, mTextColor);
mTextSize = (int) ta.getDimension(R.styleable.MyHoriztalProgressBar_progressbar_text_size, mTextSize);
mTextOffset = (int) ta.getDimension(R.styleable.MyHoriztalProgressBar_progressbar_text_offset, mTextOffset);
ta.recycle();
mPaint.setTextSize(mTextSize);
} @Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthVal = MeasureSpec.getSize(widthMeasureSpec);
int heightVal = measureHeight(heightMeasureSpec); setMeasuredDimension(widthVal, heightVal);
mRealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
} @Override
protected synchronized void onDraw(Canvas canvas) {
//draw reachBar
String text = getProgress() + "%";
int textWidth = (int) mPaint.measureText(text);
boolean noNeedUnrechBar = false; canvas.save();
canvas.translate(getPaddingLeft(), getMeasuredHeight() / 2);
float radio = getProgress() * 1.0f / getMax();
float progressX = mRealWidth * radio;
if (progressX + textWidth > mRealWidth) {
progressX = mRealWidth - textWidth;
noNeedUnrechBar = true;
}
//draw reachbar
mPaint.setColor(mReachColor);
mPaint.setStrokeWidth(mReachHeight);
float endX = progressX - mTextOffset / 2;
canvas.drawLine(0, 0, endX, 0, mPaint);
//draw text
mPaint.setColor(mTextColor);
float y = -(mPaint.descent() + mPaint.ascent())/2;
canvas.drawText(text, progressX,y, mPaint);
//draw unreachbar
if (!noNeedUnrechBar) {
mPaint.setColor(mUnReachColor);
mPaint.setStrokeWidth(mUnReachHeight);
float startX = progressX + textWidth + mTextOffset / 2;
canvas.drawLine(startX, 0, mRealWidth, 0, mPaint);
}
canvas.restore();
} private int measureHeight(int heightMeasureSpec) {
int mode = MeasureSpec.getMode(heightMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int result = 0;
if (mode == MeasureSpec.EXACTLY||mode == MeasureSpec.UNSPECIFIED) {
result = height;
} else if (mode == MeasureSpec.AT_MOST) {
int textHeight = (int) (mPaint.descent() - mPaint.ascent());
result = getPaddingTop() + getPaddingBottom() + Math.max(Math.max(mReachHeight, mUnReachHeight), textHeight);
// {
// result = Math.min(result, height);
// }
}
return result;
} protected int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
} protected int sp2px(int sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
}
}
/**
* Created by C058 on 2016/5/26.
* 模仿ios app store应用下载圆形进图条
*/
public class MyRoundProgressBar extends MyHoriztalProgressBar { private static final int DEFAULT_PROGRESS_RADIUS = 30;
private int mRadius = dp2px(DEFAULT_PROGRESS_RADIUS);
private int mInRadius;
private RectF mRectf, mInRectf; public MyRoundProgressBar(Context context) {
this(context, null);
} public MyRoundProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public MyRoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyRoundProgressBar);
mRadius = (int) ta.getDimension(R.styleable.MyRoundProgressBar_progressbar_radius, mRadius);
ta.recycle(); mReachHeight = mUnReachHeight * 2;
mPaint.setAntiAlias(true);//抗锯齿
mPaint.setDither(true); //防抖动模式
mPaint.setStyle(Paint.Style.STROKE);//画笔风格设置为空心
mPaint.setStrokeCap(Paint.Cap.ROUND);
} @Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int diameter = mRadius * 2 + getPaddingLeft() + getPaddingRight() + mUnReachHeight * 2; //控件宽度 默认四个padding一致
int width = resolveSize(diameter, widthMeasureSpec);
int height = resolveSize(diameter, heightMeasureSpec);
int realWidth = Math.min(width, height);//当宽高设置不一致,取小的那个
//外圆的半径
mRadius = (realWidth - getPaddingLeft() - getPaddingRight() - mUnReachHeight) / 2;
mRectf = new RectF(0, 0, mRadius * 2, mRadius * 2);
//内圆的半径
mInRadius = mRadius - mUnReachHeight;
mInRectf = new RectF(0, 0, mInRadius * 2, mInRadius * 2);
setMeasuredDimension(realWidth, realWidth);
} @Override
protected synchronized void onDraw(Canvas canvas) { canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
//draw unreachbar
mPaint.setColor(mUnReachColor);
mPaint.setStrokeWidth(mUnReachHeight);
//从圆点开始画圆
// canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
//draw reachbar
//将画布移动到画内圆的位置
canvas.translate(mUnReachHeight, mUnReachHeight);
mPaint.setColor(mReachColor);
mPaint.setStrokeWidth(mReachHeight);
float sweepAngle = getProgress() * 1.0f / getMax() * 360;
canvas.drawArc(mInRectf, -90, sweepAngle, false, mPaint);
// //draw text
// String text = getProgress() + "%";
// int textWidth = (int) mPaint.measureText(text);
// int textHeight = (int) ((mPaint.descent() + mPaint.ascent()) / 2);
// mPaint.setColor(mTextColor);
// canvas.drawText(text, mRadius - textWidth / 2, mRadius - textHeight, mPaint);
canvas.restore();
}
}
<declare-styleable name="MyHoriztalProgressBar">
<attr name="progressbar_reach_color" format="color" />
<attr name="progressbar_unreach_color" format="color" />
<attr name="progressbar_reach_height" format="dimension" />
<attr name="progressbar_unreach_height" format="dimension" />
<attr name="progressbar_text_color" format="color" />
<attr name="progressbar_text_size" format="dimension" />
<attr name="progressbar_text_offset" format="dimension" />
</declare-styleable>
<declare-styleable name="MyRoundProgressBar">
<attr name="progressbar_radius" format="dimension" />
</declare-styleable>
<declare-styleable name="MyHoriztalProgressBar2">
<attr name="reach_color" format="color" />
<attr name="unreach_color" format="color" />
<attr name="progressbar_height" format="dimension" />
</declare-styleable>
new Timer().schedule(new TimerTask() {
int currentIndex = (int) Math.floor(current * 100 / total);
int sumIndex = 0;
@Override
public void run() {
if (currentIndex > sumIndex) {
sumIndex = currentIndex;
LogHelp.i("polyv", "current:" + current + "-------total:" + total + "-------currentIndex:" + currentIndex);
holder.videoList_progress.setProgress(sumIndex);
}
}
}, 200, 200);
仿IOS圆形下载进度条的更多相关文章
- 在DrawingVisual上绘制圆形的进度条,类似于IOS系统风格。
1.说明:在WPF中,文件下载时需要显示下载进度,由于系统自带的条型进度条比较占用空间,改用圆形的进度条,需要在DrawingVisual上呈现. 运行的效果如图: private Point Get ...
- Android 自定义圆形旋转进度条,仿微博头像加载效果
微博 App 的用户头像有一个圆形旋转进度条的加载效果,看上去效果非常不错,如图所示: 据说 Instagram 也采用了这种效果.最近抽空研究了一下,最后实现的效果是这样: 基本上能模拟出个大概,代 ...
- 仿iReader切换皮肤进度条
仿iReader切换皮肤进度条 标签(空格分隔): 自定义View [TOC] 本以为使用paint.setXfermode(new PorterDuffXfermode(Mode.XOR));可以轻 ...
- 自定义仿 QQ 健康计步器进度条
自定义仿 QQ 健康计步器进度条 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 闲着没事,趁上班时间偷偷撸了 ...
- Xamarin iOS教程之进度条和滚动视图
Xamarin iOS教程之进度条和滚动视图 Xamarin iOS 进度条 进度条可以看到每一项任务现在的状态.例如在下载的应用程序中有进度条,用户可以很方便的看到当前程序下载了多少,还剩下多少.Q ...
- css3圆形百分比进度条的实现原理
原文地址:css3圆形百分比进度条的实现原理 今天早上起来在查看jquery插件机制的时候,一不小心点进了css3圆形百分比进度条的相关文章,于是一发不可收拾,开始折腾了... 关于圆形圈的实现,想必 ...
- 详解用CSS3制作圆形滚动进度条动画效果
主 题 今天手把手教大家用CSS3制作圆形滚动进度条动画,想不会都难!那么,到底是什么东东呢?先不急,之前我分享了一个css实现进度条效果的博客<CSS实现进度条和订单进度条>,但是呢, ...
- PHP持续保有长连接,利用flush持续更新浏览器UI,下载进度条实现
如何用PHP+JS实现上传进度条,大部分的人可能都实现过,但是下载呢?如何呢?原理也是差不多的,就是分次读写,每次读多少字节,但是这样的不好就是长连接,一般实现下载进度条常用的两种解决方案是:一种是需 ...
- 用 CALayer 定制下载进度条控件
// // RPProgressView.h // CALayer定制下载进度条控件 // // Created by RinpeChen on 16/1/2. // Copyright © 2016 ...
随机推荐
- Urllib2 总结
Urllib2 总结 介绍 Urllib2是用于获取URLs(统一资源定位符)的一个Python模块.它以urlopen函数的形式提供了非常简单的接口.能够使用各种不同的协议来获取网址.它还提供一个稍 ...
- Effective C++ -----条款38:通过复合塑模出has-a或“根据某物实现出”
复合(composition)的意义和public继承完全不同. 在应用域(application domain),复合意味has-a(有一个).在实现域(implementation domain) ...
- c#操作时间
本年还剩下多少天 private string GetEndTime() { DateTime dt = DateTime.Now; DateTime startYear = DateTime.Now ...
- Java Collection、Map集合总结
1.Java中的Collection集合总结 Collection |---List(存储有序,可重复) |---ArrayList 底层数据结构是数组,查询快,增删慢. 线程不安全.效率高 |--- ...
- oracle默认配置ora文件位置
unix:$ORACLE_HOME/dbsnt:c:\Oracle\ora81\database create spfile from pfile = '/home/oracle/initora11g ...
- 模拟赛1029d2
[问题描述]祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到轨道上并加入原有序列中.一旦有三个或更多同色的珠子变 ...
- 简单获取input file 选中的图片,并在一个div的img里面赋值src实现预览图片
html代码: <input id="file_upload" type="file" /> <div class="image_c ...
- python基础——map/reduce
python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...
- FILE文件操作
http://www.jb51.net/article/37688.htm fopen(打开文件)相关函数 open,fclose表头文件 #include<stdio.h>定义函数 FI ...
- IPC进程通信机制
select.poll.epoll之间的区别总结[整理] 进程间通信---共享内存 信号量和互斥锁的区别 http://www.2cto.com/os/201510/445553.html http: ...