博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
仿IOS圆形下载进度条
阅读量:4977 次
发布时间:2019-06-12

本文共 8331 字,大约阅读时间需要 27 分钟。

/** * 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();    }}
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);

 

转载于:https://www.cnblogs.com/anni-qianqian/p/5827747.html

你可能感兴趣的文章
GNSS 使用DFT算法 能量损耗仿真
查看>>
【转】Simulink模型架构指导
查看>>
[转载]java开发中的23种设计模式
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>
springboot+mybatis报错Invalid bound statement (not found)
查看>>
Linux环境下SolrCloud集群环境搭建关键步骤
查看>>
MongoDB的简单使用
查看>>
【noip2004】虫食算——剪枝DFS
查看>>
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
SQL中Group By的使用
查看>>
两个表格中数据不用是一一对应关系--来筛选不同数据,或者相同数据
查看>>
js05-DOM对象二
查看>>
mariadb BINLOG_FORMAT = STATEMENT 异常
查看>>
Java回顾之多线程
查看>>
机电行业如何进行信息化建设
查看>>
2018 Multi-University Training Contest 10 - Count
查看>>
HDU6203 ping ping ping
查看>>
Fireworks基本使用
查看>>