Android自定義圓環式進度條

安卓自定義圓環式進度條,供大傢參考,具體內容如下

需求是實現一個圓環式中間帶有進度的進度條,自己動手實現一個

package com.djt.aienglish.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.djt.aienglish.R;


/**
 * @author qiu
 * @date 2020/3/12 13:51
 */
public class CirclePgBar extends View {
    private int mHeight = 0;
    private int mWidth = 0;

    // 畫圓環的畫筆
    private Paint mRingPaint;
    // 畫圓環的畫筆背景色
    private Paint mRingPaintBg;
    // 畫字體的畫筆
    private Paint mTextPaint;
    // 圓環顏色
    private int mRingColor;
    // 圓環背景顏色
    private int mRingBgColor;
    // 半徑
    private float mRadius;
    // 圓環半徑
    private float mRingRadius;
    // 圓環寬度
    private float mStrokeWidth;
    // 圓心x坐標
    private int mXCenter;
    // 圓心y坐標
    private int mYCenter;
    // 字的長度
    private float mTxtWidth;
    // 字的高度
    private float mTxtHeight;
    // 總進度
    private int max = 100;
    // 當前進度
    private int progress;
    private String text;

    public CirclePgBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 獲取自定義的屬性
        initAttrs(context, attrs);
        initVariable();
    }

    /**
     * 屬性
     */
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TasksCompletedView, 0, 0);
        mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_circleWidth, 0);
        mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFFFFFFFF);
        mRingBgColor = typeArray.getColor(R.styleable.TasksCompletedView_ringBgColor, 0xFFFFFFFF);
        text = typeArray.getString(R.styleable.TasksCompletedView_text);
        max = typeArray.getInteger(R.styleable.TasksCompletedView_max, 0);
        progress = typeArray.getInteger(R.styleable.TasksCompletedView_progress, 0);
    }

    /**
     * 初始化畫筆
     */
    private void initVariable() {
        //外圓弧背景
        mRingPaintBg = new Paint();
        mRingPaintBg.setAntiAlias(true);
        mRingPaintBg.setColor(mRingBgColor);
        mRingPaintBg.setStyle(Paint.Style.STROKE);
        mRingPaintBg.setStrokeWidth(mStrokeWidth);
        //外圓弧
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth);
        //mRingPaint.setStrokeCap(Paint.Cap.ROUND);//設置線冒樣式,有圓 有方
        //中間字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);
        invalidate();
    }

    //測量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //實際測量寬高
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        if (mWidth > mHeight) {
            mRadius = mHeight / 2;
        } else {
            mRadius = mWidth / 2;
        }
        //半徑
        mRingRadius = mRadius - mStrokeWidth / 2;
        //文字寬高測量
        mTextPaint.setTextSize(mRadius / 2);
        Paint.FontMetrics fm = mTextPaint.getFontMetrics();
        mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
    }

    /**
     * 畫圖
     */
    @Override
    protected void onDraw(Canvas canvas) {
        mXCenter = mWidth / 2;
        mYCenter = mHeight / 2;
        //外圓弧背景
        RectF rectBg = new RectF(mXCenter - mRingRadius, mYCenter - mRingRadius, mXCenter + mRingRadius, mYCenter + mRingRadius);
        canvas.drawArc(rectBg, 0, 360, false, mRingPaintBg);
        //外圓弧//進度
        if (progress > 0) {
            RectF oval = new RectF(mXCenter - mRingRadius, mYCenter - mRingRadius, mXCenter + mRingRadius, mYCenter + mRingRadius);
            canvas.drawArc(oval, -90, ((float) progress / max) * 360, false, mRingPaint);
        }
        //字體
        if(!TextUtils.isEmpty(text)) {
            mTxtWidth = mTextPaint.measureText(text, 0, text.length());
            canvas.drawText(text, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
        }
    }

    /**
     * 設置進度
     *
     * @param progress
     */
    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();//重繪
    }

    /**
     * 設置最大值
     *
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
        postInvalidate();
    }

    /**
     * 設置文字內容
     *
     * @param text
     */
    public void setText(String text) {
        this.text = text;
        postInvalidate();
    }
}

別忘記在value下的attr.xml中加入默認配置屬性

<!--圓弧進度條-->
    <declare-styleable name="TasksCompletedView">
        <attr name="circleWidth" format="dimension" />
        <attr name="ringColor" format="color" />
        <attr name="ringBgColor" format="color" />
        <attr name="text" format="string" />
        <attr name="progress" format="integer" />
     <attr name="max" format="integer" />
</declare-styleable>

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。