Android开发 View_自定义圆环进度条View

前言

  一个实现,空心圆环的自定义View,已经封装完好,可以直接使用。

 

效果图

Android开发 View_自定义圆环进度条View

代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

public class RingProgressBar extends View {
    private Paint mPaint;
    private int mBgRingColor = Color.BLACK;
    private int mProgressRingColor = Color.RED;
    private int mBgRingWidth = 20;
    private int mProgressRingWidth = 20;
    private int mMax = 100;
    private int mCurrentProgress = 0;
    private int width = 0;
    private int height = 0;


    public RingProgressBar(Context context) {
        super(context);
    }

    public RingProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
    }

    public RingProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
    }

    public RingProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mPaint = new Paint();
    }

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

    /**
     * 测量尺寸
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    public void setBgRing(@ColorInt int bgRingColor, int ringWidth) {
        mBgRingColor = bgRingColor;
        mBgRingWidth = dip2px(ringWidth);
        postInvalidate();
    }

    public void setProgressRing(@ColorInt int progressRingColor, int ringWidth) {
        mProgressRingColor = progressRingColor;
        mProgressRingWidth = dip2px(ringWidth);
        postInvalidate();

    }

    /**
     * 设置最大值
     *
     * @param max
     */
    public void setMax(int max) {
        mMax = max;
        postInvalidate();

    }

    /**
     * 设置进度
     *
     * @param progress 
     */
    public void setProgress(int progress) {
        mCurrentProgress = progress;
        postInvalidate();   //请求失效,重新绘制

    }

    /**
     * 绘制背景环
     * @param canvas
     */
    private void drawBgRing(Canvas canvas) {
        mPaint.setColor(mBgRingColor);      //设置画笔颜色
        mPaint.setStyle(Paint.Style.STROKE);//设置画笔为描边
        mPaint.setStrokeWidth(mBgRingWidth);//设置描边宽度
        mPaint.setStrokeCap(Paint.Cap.BUTT);//设置画笔收笔类型
        mPaint.setAntiAlias(true);          //抗锯齿
        canvas.drawCircle(width / 2, height / 2, (width - mBgRingWidth) / 2, mPaint);   //画圆形
        mPaint.reset();
    }

    /**
     * 绘制进度环
     * @param canvas
     */
    private void drawProgressRing(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.BUTT);
        mPaint.setColor(mProgressRingColor);
        mPaint.setStrokeWidth(mProgressRingWidth);
        mPaint.setAntiAlias(true);
        RectF rectF = new RectF();
        rectF.left = 0 + mProgressRingWidth / 2;
        rectF.right = width - mProgressRingWidth / 2;
        rectF.top = 0 + mProgressRingWidth / 2;
        rectF.bottom = height - mProgressRingWidth / 2;
        float progress = 0;
        if (mCurrentProgress < mMax) {
            float proportion = (float) 360 / (float) mMax;
            progress = mCurrentProgress * proportion;
        } else {
            progress = 360;
        }
        canvas.drawArc(rectF, 270, progress, false, mPaint);    //画圆弧
        mPaint.reset();
    }

    /**
     * 根据手机分辨率从DP转成PX
     * @param dpValue
     * @return
     */
    public int dip2px(float dpValue) {
        float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

Android开发 View_自定义圆环进度条View

上一篇:diango创建一个app


下一篇:HttpCanary——最强Android抓包工具!