Android自定义View-圆形加载进度条

效果

  • CircleProgressbarView
/**
 * 圆形加载 ProgressBar
 *
 * @attr ref R.styleable.CircleProgressBarView_styleable#styleable_id
 * @attr ref R.styleable.CircleProgressBarView_round_color#round_color
 * @attr ref R.styleable.CircleProgressBarView_progress_color#progress_color
 * @attr ref R.styleable.CircleProgressBarView_round_width#round_width
 * @attr ref R.styleable.CircleProgressBarView_max#max
 * @attr ref R.styleable.CircleProgressBarView_progress#progress
 * @attr ref R.styleable.CircleProgressBarView_style#style
 */

public class CircleProgressBarView extends View {

    private static final String TAG = "CircleProgressBarView";

    private Paint mPaint;

    private int roundColor;

    private int progressColor;

    private float roundWidth;

    private int max;

    private int progress;

    public static final int STYLE_STROKE = 0;
    public static final int STYLE_FILL = 1;

    private int style = STYLE_STROKE;

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

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

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

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBarView_styleable);
        roundColor = typedArray.getColor(R.styleable.CircleProgressBarView_styleable_CircleProgressBarView_round_color, 0);
        progressColor = typedArray.getColor(R.styleable.CircleProgressBarView_styleable_CircleProgressBarView_progress_color, 0);
        roundWidth = typedArray.getDimension(R.styleable.CircleProgressBarView_styleable_CircleProgressBarView_round_width, 3);
        max = typedArray.getInteger(R.styleable.CircleProgressBarView_styleable_CircleProgressBarView_max, 100);
        progress = typedArray.getInteger(R.styleable.CircleProgressBarView_styleable_CircleProgressBarView_progress, 0);
        style = typedArray.getInt(R.styleable.CircleProgressBarView_styleable_CircleProgressBarView_style, 0);

        typedArray.recycle();
    }


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

        int center = getWidth() / 2;
        if (getWidth() > getHeight()) {
            center = getHeight() / 2;
        }
        int radius = (int) (center - roundWidth / 2);
        mPaint.setAntiAlias(true);
        mPaint.setColor(roundColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(roundWidth);
        canvas.drawCircle(center, center, radius, mPaint);

        mPaint.setStrokeWidth(roundWidth);
        mPaint.setColor(progressColor);
        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
        switch (style) {
            case STYLE_STROKE:
                mPaint.setStyle(Paint.Style.STROKE);
                canvas.drawArc(oval, 270, 360 * progress / max, false, mPaint);
                break;
            case STYLE_FILL:
                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                canvas.drawArc(oval, 270, 360 * progress / max, true, mPaint);
                break;
            default:
                break;
        }
    }

    public synchronized int getMax() {
        return max;
    }

    public synchronized void setProgress(int progress) {
        if (progress < 0) {
            progress = 0;
        }
        if (progress > max) {
            progress = max;
        }
        this.progress = progress;
        postInvalidate();
    }
}

  • attrs
    <declare-styleable name="CircleProgressBarView_styleable">
        <attr name="CircleProgressBarView_round_color" format="color" />
        <attr name="CircleProgressBarView_progress_color" format="color" />
        <attr name="CircleProgressBarView_round_width" format="dimension" />
        <attr name="CircleProgressBarView_max" format="integer" />
        <attr name="CircleProgressBarView_progress" format="integer" />
        <attr name="CircleProgressBarView_style">
            <enum name="STROKE" value="0" />
            <enum name="FILL" value="1" />
        </attr>
    </declare-styleable>
  1. 在onDraw中首先绘制圆环,使用

     canvas.drawCircle(center, center, radius, mPaint);
    
  2. 计算绘制角度

     360 * progress / max
    
  3. 绘制进度

     canvas.drawArc(oval, 270, 360 * progress / max, false, mPaint);
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值