仿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 ...
随机推荐
- Win8环境WPF打开和关闭软键盘
代码如下: public class KeyBoardHelper { #region 键盘控制 /// <summary> /// 显示键盘 /// </summary> p ...
- suse11 sp2 搭建openvpn
什么是VPN IP机制仿真出一个私有的广域网"是通过私有的隧道技术在公共数据网络上仿真一条点到点的专线技术.所谓虚拟,是指用户不再需要拥有实际的长途数据线路,而是使用Internet公众数据 ...
- HDU 3111 Sudoku(精确覆盖)
数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... #include <cs ...
- Mathematics:Find a multiple(POJ 2356)
找组合 题目大意:给你N个自然数,请你求出若干个数的组合的和为N的整数倍的数 经典鸽巢原理题目,鸽巢原理的意思是,有N个物品,放在N-1个集合中,则一定存在一个集合有2个元素或以上. 这一题是说有找出 ...
- 数据结构-链表逆置(c++模板类实现)
链表结点类模板定义: template <class T> class SingleList; template <class T> class Node { private: ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- centos 6.5 配置LDAP服务器+客户端!
各种度娘!各种歌哥!网上教程参差不齐,历时1天,终于完成,不敢独享,遂,总结分享之,有问题可以留言,知无不言...开始吧 Note: 本次配置的服务器环境是<redhat enterprise ...
- Html5 新标签
⒈ <audio></audio> 定义声音<autoplay></autoplay> 该属性出现,音频就绪后马上播放<controls>& ...
- August 18th 2016 Week 34th Thursday
Comedy is acting out optimism. 喜剧就是将乐观演绎出来. Being optimistic or pessimistic, that is all about your ...
- 选择题(codevs 2919)
2919 选择题 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 某同学考试,在N*M的答题卡上写 ...