欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

带分割线的百分比进度条

程序员文章站 2022-06-09 19:09:25
带分割线的百分比进度条// the codepublic class WProgressBar extends View { private Paint rightPaint; private Paint leftPaint; private int widthSize; private int heightSize; private int hDefaultSize; private int wDefaultSize; private Rect...

带分割线的百分比进度条

// 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(Color.RED);
        leftPaint.setColor(Color.GREEN);
        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);

        leftPath.addArc(leftPoint,90,180);
        leftPath.moveTo(leftArea.left, leftArea.top);
        leftPath.lineTo(leftArea.right - spaceSize, 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;
        rightArea.top = leftArea.top;
        rightArea.right = rightPoint.left + heightSize / 2f;
        rightArea.bottom = getPaddingTop() + heightSize;

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

带分割线的百分比进度条

本文地址:https://blog.csdn.net/flying_fish_wj/article/details/114309309

相关标签: android java