自定义圆圈ProgressBar

最近项目需要整一个清理内存的圆圈pro,要求中间还要带个文本展示

样式如下:

然而仅仅只是这样一个样式怎么能够满足一个码农的发散性思维

于是就有了下面的样式

然后来看一下xml的使用

<com.hwj.gui.ui.LoadingProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lpb_clean"
    android:layout_width="180dp"
    android:layout_height="180dp"
    android:layout_centerHorizontal="true"
    android:progress="66"
    android:textColor="@color/colorPrimary"
    android:textSize="50dp"
    app:progressBackground="@android:color/holo_blue_bright"
    app:progressColor="#3BCFC9"
    app:progressWidth="10dp" />

emm……

有点简陋哈哈哈哈

好了重要的部分来了

上源码

package com.hwj.gui.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.hwj.gui.R;

/**
 * CreateBy:  MR.LEE
 * date:      2020/6/24_14:14
 */
public class LoadingProgressBar extends View {

    private int progress;
    private float progressWidth;
    private float textMaxWidth;
    private RectF progressRectF;
    private Paint progressPaint;
    private float width;
    private float height;
    private Paint textPaint;
    private int progressColor;
    private int progressBackground;
    private Rect rect;

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

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

    public LoadingProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (context == null)
            return;
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LoadingProgressBar);
        float textSize = array.getDimension(R.styleable.LoadingProgressBar_android_textSize, 0);
        progressColor = array.getColor(R.styleable.LoadingProgressBar_progressColor, Color.GRAY);
        int textColor = array.getColor(R.styleable.LoadingProgressBar_android_textColor, progressColor);
        progress = array.getInteger(R.styleable.LoadingProgressBar_android_progress, 0);
        progressBackground = array.getColor(R.styleable.LoadingProgressBar_progressBackground, Color.TRANSPARENT);
        progressWidth = array.getDimension(R.styleable.LoadingProgressBar_progressWidth, 1);
        array.recycle();
        TextView textView = new TextView(context);
        textView.setTextSize(textSize);
        textMaxWidth = textView.getPaint().measureText("100%");
        progressRectF = new RectF();
        rect = new Rect();
        progressPaint = new Paint();
        progressPaint.setColor(progressColor); //设置画笔颜色
        progressPaint.setStyle(Paint.Style.STROKE); //设置填充样式
        progressPaint.setStrokeWidth(progressWidth); //设置画笔宽度
        progressPaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setColor(textColor); //设置画笔颜色
        textPaint.setStyle(Paint.Style.FILL); //设置填充样式
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST) {
            width = getPaddingLeft() + getPaddingRight() + textMaxWidth + progressWidth * 2;
        } else {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }

        if (heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.AT_MOST) {
            height = getPaddingTop() + getPaddingBottom() + textMaxWidth + progressWidth * 2;
        } else {
            height = MeasureSpec.getSize(heightMeasureSpec);
        }

        setMeasuredDimension((int) width, (int) height);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    }

    public int getProgress() {
        return progress;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        progressRectF.left = getPaddingLeft() + progressWidth / 2f;
        progressRectF.top = getPaddingTop() + progressWidth / 2f;
        progressRectF.right = width - getPaddingRight() - progressWidth / 2f;
        progressRectF.bottom = height - getPaddingBottom() - progressWidth / 2f;
        if (progress > 100)
            progress = 100;

        if (progress < 0) {
            progress = 0;
        }

        if (progress == 0)
            return;
        double sweepAngle = 360d * progress / 100;

        progressPaint.setColor(progressBackground);
        canvas.drawArc(progressRectF, 0, 360, false, progressPaint);

        progressPaint.setColor(progressColor);
        canvas.drawArc(progressRectF, 90, (float) sweepAngle, false, progressPaint);

        String str = progress + "%";
        textPaint.getTextBounds(str, 0, str.length(), rect);

        int w = rect.width(); //获取宽度

        int h = rect.height();//获取高度

//        真实宽高
        width = progressRectF.right - progressRectF.left;
        height = progressRectF.bottom - progressRectF.top;
        float startX = width - w < 0 ? 0f : width - w;
        float startY = height + h > height * 2 ? height * 2 : width + h;
        canvas.drawText(str, (startX + getPaddingLeft()) / 2f + progressRectF.left,
                (startY + getPaddingTop()) / 2f + progressRectF.top, textPaint);
    }
}

接下来就是attrs.xml了

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="LoadingProgressBar">
        <attr name="android:textSize" />
        <attr name="android:textColor" />
        <attr name="android:progress" />
        <attr name="progressColor" format="color" />
        <attr name="progressBackground" format="color" />
        <attr name="progressWidth" format="dimension" />
    </declare-styleable>
</resources>

打完收工

揍是这么easy

留下以备不时之需,有需要可以拿去耍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值