Android,手写一个进度条,自定义View

效果图

0.png
1.png
2.png
/**
 * @author : isShuai
 * @Date : 2021/3/26
 * 带文字的进度条
 */
public class TextProgressView extends View {
    private int max = 100;
    private int progress = 0;
    private int width = 0;
    private final int height = 120;
    private final int roundBackColor = Color.parseColor("#e6e6e6");
    private final int progressColor = ViewUtils.getColor(R.color.blue_2863fa);
    private final RectF rect = new RectF();
    private final Rect textRect = new Rect();
    private final Path path = new Path();
    private final int textSize = 40;
    private final int circleR = 10;

    private Paint mPaint = new Paint();
    private Paint textPaint = new Paint();
    private float textRectWidth;

    public TextProgressView(Context context) {
        this(context, null);
    }

    public TextProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(textSize);
        textPaint.setColor(Color.WHITE);
        textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
        String maxString = "100%";
        textPaint.getTextBounds(maxString, 0, maxString.length(), textRect);
        textRectWidth = (float) textRect.width() / 2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int w;
        int h;
        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
            w = Utils.getWindowWidth(getContext());
        } else w = widthMeasureSpec;

        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            h = height;
        } else h = heightMeasureSpec;
        setMeasuredDimension(w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        float p = ((float) progress / max);
        float currProgress = p * (width - (textRectWidth * 2)) + textRectWidth;
        String text = ((int) (p * 100)) + "%";

        //画背景
        mPaint.setColor(roundBackColor);
        rect.left = textRectWidth;
        rect.top = 90;
        rect.right = width - textRectWidth;
        rect.bottom = 120;
        canvas.drawRoundRect(rect, 50, 50, mPaint);

        //画进度
        mPaint.setColor(progressColor);
        rect.left = textRectWidth;
        rect.top = 90;
        rect.right = currProgress;
        rect.bottom = 120;
        canvas.drawRoundRect(rect, 50, 50, mPaint);

        //圆点
        mPaint.setColor(Color.WHITE);
        float circleWidth = currProgress - circleR - 15;
        if (circleWidth > textRectWidth){
            canvas.drawCircle(circleWidth, 105, circleR, mPaint);
        }else
            circleWidth = textRectWidth;

        textPaint.getTextBounds(text, 0, text.length(), textRect);

        //画进度文字背景
        mPaint.setColor(progressColor);
        rect.left = circleWidth - ((float) textRect.width() / 2) - 20;
        rect.top = 5;
        rect.right = circleWidth + ((float) textRect.width() / 2) + 20;
        rect.bottom = 70;
        canvas.drawRoundRect(rect, 10, 10, mPaint);

        //画进度文字
        float textWidth = circleWidth - ((float) textRect.width() / 2);
        canvas.drawText(text, textWidth, 55, textPaint);

        //画进度文字背景底部三角
        mPaint.setColor(progressColor);
        path.reset();
        path.moveTo( circleWidth, 80);
        path.lineTo(circleWidth +15, 70);
        path.lineTo(circleWidth -15, 70);
        path.lineTo(circleWidth, 80);
        path.close();
        canvas.drawPath(path, mPaint);
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setProgress(int progress) {
        if (progress > max) {

        } else {
            this.progress = progress;
            invalidate();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getWidth();
        Log.e("yxs", "大小变化:");
    }

}


可以的话请点个赞,谢谢!

你可能感兴趣的:(Android,手写一个进度条,自定义View)