首页 > 代码库 > 仿IOS圆形下载进度条
仿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圆形下载进度条
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。