自定义圆形加载进度条

自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="RoundProgressBar">
        <attr name="progress_radius" format="dimension" />
        <attr name="progress_color" format="color" />
        <attr name="paint_width" format="dimension" />
        <attr name="text_color" format="color" />
        <attr name="text_width" format="dimension" />
        <attr name="text_draw" format="boolean" />
    </declare-styleable>

</resources>

创建圆形进度条类

public class RoundProgressBar extends ProgressBar {

    // 画笔大小
    private int paintWidth = dp2px(4);
    // 字体大小
    private int textSize = sp2px(20);
    // 字体颜色
    private int textColor = 0xAA0000FF;
    // 是否需要绘制进度值
    private boolean drawText = true;
    // ProgressBar的半径
    private int progressRadius = dp2px(30);
    // ProgressBar的颜色
    private int progressColor = 0xAA0000FF;

    private Paint paint;
    private RectF rectF;

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

    public RoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        obtainStyleAttrs(context, attrs);
    }

    private void obtainStyleAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
        paintWidth = (int) ta.getDimension(R.styleable.RoundProgressBar_paint_width, paintWidth);
        textSize = (int) ta.getDimension(R.styleable.RoundProgressBar_text_width, textSize);
        textColor = ta.getColor(R.styleable.RoundProgressBar_text_color, textColor);
        drawText = ta.getBoolean(R.styleable.RoundProgressBar_text_draw, drawText);
        progressRadius = (int) ta.getDimension(R.styleable.RoundProgressBar_progress_radius, progressRadius);
        progressColor = ta.getColor(R.styleable.RoundProgressBar_progress_color, progressColor);

        paint = new Paint();
        // 抗锯齿
        paint.setAntiAlias(true);
        // 防止抖动
        paint.setDither(true);
        // 线头为圆角
        paint.setStrokeCap(Paint.Cap.ROUND);

        ta.recycle();
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expect = progressRadius * 2 + paintWidth + getPaddingTop() + getPaddingBottom();
        int width = resolveSize(expect, widthMeasureSpec);
        int height = resolveSize(expect, heightMeasureSpec);
        // 绘制区域
        int realArea = Math.min(width, height);
        // ProgressBar的半径
        progressRadius = (realArea - getPaddingTop() - getPaddingBottom() - paintWidth) / 2;
        rectF = new RectF(0, 0, progressRadius * 2, progressRadius * 2);
        setMeasuredDimension(realArea, realArea);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // 进度值
        String text = getProgress() + "%";
        float textWidth = paint.measureText(text);
        float textHeight = (paint.descent() + paint.ascent()) / 2;

        canvas.save();
        // 开始绘制的偏移
        canvas.translate(getPaddingLeft() + paintWidth / 2, getPaddingTop() + paintWidth / 2);
        // 进度值对应的弧度
        float angle = getProgress() * 1.0f / getMax() * 360;
        paint.setColor(progressColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) paintWidth);
        // 绘制进度圆弧
        canvas.drawArc(rectF, 0, angle, false, paint);

        // 绘制进度值
        if (drawText) {
            paint.setColor(textColor);
            paint.setTextSize(textSize);
            paint.setStyle(Paint.Style.FILL);
            canvas.drawText(text, progressRadius - textWidth / 2, progressRadius - textHeight, paint);
        }
        canvas.restore();
    }

    private int dp2px(int value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                getResources().getDisplayMetrics());
    }

    private int sp2px(int value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value,
                getResources().getDisplayMetrics());
    }

}

在布局文件中引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.zc.myapp.views.demo.RoundProgressBar
        android:id="@+id/progress"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="30dp"
        android:layout_gravity="center_horizontal"
        app:paint_width="4dp"
        app:progress_color="#77FF0000"
        app:progress_radius="30dp"
        app:text_color="#77FF0000"
        app:text_width="20sp"
        app:text_draw="true"
        android:progress="0" />

</LinearLayout>

在Activity中使用

public class MainActivity extends AppCompatActivity {

    private RoundProgressBar progressBar;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            int progress = progressBar.getProgress();
            progressBar.setProgress(++progress);
            if (progress >= 100) {
                handler.removeMessages(111);
            }
            handler.sendEmptyMessageDelayed(111, 100);
        }
    };

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = findViewById(R.id.progress);
        handler.sendEmptyMessage(111);
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容