android圆弧加载view,Android自定义View-圆形加载进度条

本文详细介绍了如何创建一个自定义的CircleProgressBarView组件,该组件用于显示圆形加载进度。通过解析XML属性,设置了圆环颜色、进度颜色、圆环宽度、最大值和当前进度,并提供了两种样式:描边和填充。在onDraw方法中,根据进度绘制了相应的圆弧,实现了进度条效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

55e3d5612b61

效果

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

在onDraw中首先绘制圆环,使用

canvas.drawCircle(center, center, radius, mPaint);

计算绘制角度

360 * progress / max

绘制进度

canvas.drawArc(oval, 270, 360 * progress / max, false, mPaint);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值