带分割线的百分比进度条

带分割线的百分比进度条

// the code
public class WProgressBar extends View {

    private Paint rightPaint;
    private Paint leftPaint;
    private int widthSize;
    private int heightSize;
    private int hDefaultSize;
    private int wDefaultSize;
    private RectF rightArea;
    private int spaceSize;
    private Path rightPath;
    private Path leftPath;
    private RectF leftArea;
    private float progress;
    private final static float MAX = 100;
    private RectF leftPoint;
    private RectF rightPoint;


    public WProgressBar(Context context) {
        super(context);
        init();
    }

    public WProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public float getProgress() {
        return progress;
    }

    public void setProgress(float progress) {
      
        this.progress = Math.min(progress, MAX);
        postInvalidate();
    }


    private void init() {
        rightPaint = new Paint();

        rightPaint.setAntiAlias(true);
        leftPaint = new Paint();
        leftPaint.setAntiAlias(true);
        leftArea = new RectF();
        rightArea = new RectF();
        leftPoint = new RectF();
        rightPoint = new RectF();
        spaceSize = HtscSystemUtil.convertDpToPixel(5);
        rightPath = new Path();
        leftPath = new Path();
        progress = 50f;
        hDefaultSize = HtscSystemUtil.convertDpToPixel(6);
        wDefaultSize = HtscSystemUtil.convertDpToPixel(0);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        heightSize = MeasureSpec.getSize(heightMeasureSpec);

        widthSize = widthMode == MeasureSpec.EXACTLY ? widthSize : wDefaultSize;
        heightSize = heightMode == MeasureSpec.EXACTLY ? heightSize : hDefaultSize;

        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawView(canvas);
    }

    private void drawView(Canvas canvas) {

        int spaceLength = (widthSize - getPaddingLeft() - getPaddingRight() - spaceSize - heightSize);
        rightPaint.setColor(HtscColorUtil.zlgDownPriceColor);
        leftPaint.setColor(HtscColorUtil.zlgUpPriceColor);
        leftPoint.left = getPaddingLeft();
        leftPoint.right = leftPoint.left + heightSize;
        leftPoint.top = getPaddingTop();
        leftPoint.bottom = getPaddingTop() + heightSize;

        leftArea.left = leftPoint.right - heightSize / 2f;
        leftArea.top = getPaddingTop();
        leftArea.bottom = getPaddingTop() + heightSize;
        leftArea.right = leftArea.left + spaceLength * (progress / MAX) + spaceSize / 2f;

        leftPath.addArc(leftPoint,90,180);
        leftPath.moveTo(leftArea.left,leftArea.top);
        leftPath.lineTo(leftArea.right - spaceSize / 2f, leftArea.top);
        leftPath.lineTo(leftArea.right,leftArea.bottom);
        leftPath.lineTo(leftArea.left, leftArea.bottom);
        canvas.drawPath(leftPath, leftPaint);


        rightPoint.left = widthSize - getPaddingRight() - heightSize;
        rightPoint.top = leftArea.top;
        rightPoint.bottom = leftArea.bottom;
        rightPoint.right = widthSize - getPaddingRight();


        rightArea.left = leftArea.right + spaceSize /2f;
        rightArea.top = leftArea.top;
        rightArea.right = rightArea.left  + (1f - progress / MAX) * spaceLength;
        rightArea.bottom = getPaddingTop() + heightSize;

        rightPath.addArc(rightPoint,-90,180);
        rightPath.moveTo(rightArea.left - spaceSize / 2f, rightArea.top);
        rightPath.lineTo(rightArea.right , rightArea.top);
        rightPath.lineTo(rightArea.right, rightArea.bottom);
        rightPath.lineTo(rightArea.left, rightArea.bottom);
        canvas.drawPath(rightPath, rightPaint);
    }
}

在这里插入图片描述

已标记关键词 清除标记
相关推荐
<p> <b><span style="background-color:#FFE500;">【超实用课程内容】</span></b> </p> <p> <br /> </p> <p> <br /> </p> <p> 本课程内容包含讲解<span>解读Nginx的基础知识,</span><span>解读Nginx的核心知识、领学员进行</span>高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 </p> <p> <br /> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><br /> </b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><span style="background-color:#FFE500;">【课程如何观看?】</span></b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> PC端:<a href="https://edu.csdn.net/course/detail/26277"><span id="__kindeditor_bookmark_start_21__"></span></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 移动端:CSDN 学院APP(注意不是CSDN APP哦) </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ </p> <p style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <br /> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <strong><span style="background-color:#FFE500;">【学员专享增值服务】</span></strong> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b>源码开放</b> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 下载方式:电脑登录<a href="https://edu.csdn.net/course/detail/26277"></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a>,播放页面右侧点击课件进行资料打包下载 </p> <p> <br /> </p> <p> <br /> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页