本文介绍自定义圆形进度条的实现,主要利用自定义 View 和动画相关知识。
实现步骤分析
- 准备好外层圆、内层圆、中间文字的画笔
- 测量
- 绘制
- 提供相关方法
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 获取自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar);
mInnerBackground = array.getColor(R.styleable.ProgressBar_innerBackground, mInnerBackground);
mOuterBackground = array.getColor(R.styleable.ProgressBar_outerBackground, mOuterBackground);
mRoundWidth = (int) array.getDimension(R.styleable.ProgressBar_roundWidth, dip2px(10));
mProgressTextSize = array.getDimensionPixelSize(R.styleable.ProgressBar_progressTextSize,
sp2px(mProgressTextSize));
mProgressTextColor = array.getColor(R.styleable.ProgressBar_progressTextColor, mProgressTextColor);
array.recycle();
mInnerPaint = new Paint();
mInnerPaint.setAntiAlias(true);
mInnerPaint.setColor(mInnerBackground);
mInnerPaint.setStrokeWidth(mRoundWidth);
mInnerPaint.setStyle(Paint.Style.STROKE);
mOuterPaint = new Paint();
mOuterPaint.setAntiAlias(true);
mOuterPaint.setColor(mOuterBackground);
mOuterPaint.setStrokeWidth(mRoundWidth);
mOuterPaint.setStyle(Paint.Style.STROKE);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(mProgressTextColor);
mTextPaint.setTextSize(mProgressTextSize);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 只保证是正方形
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(Math.min(width, height), Math.min(width, height));
}
@Override
protected void onDraw(Canvas canvas) {
// 先画内圆
int center = getWidth() / 2;
canvas.drawCircle(center, center, center - mRoundWidth / 2, mInnerPaint);
// 画外圆,画圆弧
RectF rect = new RectF(0 + mRoundWidth / 2, 0 + mRoundWidth / 2,
getWidth() - mRoundWidth / 2, getHeight() - mRoundWidth / 2);
if (mProgress == 0) {
return;
}
float percent = (float) mProgress / mMax;
canvas.drawArc(rect, 0, percent * 360, false, mOuterPaint);
// 画进度文字
String text = ((int) (percent * 100)) + "%";
Rect textBounds = new Rect();
mTextPaint.getTextBounds(text, 0, text.length(), textBounds);
int x = getWidth() / 2 - textBounds.width() / 2;
Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
int baseLineY = getHeight() / 2 + dy;
canvas.drawText(text, x, baseLineY, mTextPaint);
}
public synchronized void setMax(int max) {
if (max < 0) {
}
this.mMax = max;
}
public synchronized void setProgress(int progress) {
if (progress < 0) {
}
this.mProgress = progress;
// 刷新 invalidate
invalidate();
}
public class ProgressBar extends View {
private int mInnerBackground = Color.RED;
private int mOuterBackground = Color.RED;
private int mRoundWidth = 10; // 10px
private float mProgressTextSize = 15;
private int mProgressTextColor = Color.RED;
private Paint mInnerPaint, mOuterPaint, mTextPaint;
private int mMax = 100;
private int mProgress = 0;
public ProgressBar(Context context) {
this(context, null);
}
public ProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 获取自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressBar);
mInnerBackground = array.getColor(R.styleable.ProgressBar_innerBackground, mInnerBackground);
mOuterBackground = array.getColor(R.styleable.ProgressBar_outerBackground, mOuterBackground);
mRoundWidth = (int) array.getDimension(R.styleable.ProgressBar_roundWidth, dip2px(10));
mProgressTextSize = array.getDimensionPixelSize(R.styleable.ProgressBar_progressTextSize,
sp2px(mProgressTextSize));
mProgressTextColor = array.getColor(R.styleable.ProgressBar_progressTextColor, mProgressTextColor);
array.recycle();
mInnerPaint = new Paint();
mInnerPaint.setAntiAlias(true);
mInnerPaint.setColor(mInnerBackground);
mInnerPaint.setStrokeWidth(mRoundWidth);
mInnerPaint.setStyle(Paint.Style.STROKE);
mOuterPaint = new Paint();
mOuterPaint.setAntiAlias(true);
mOuterPaint.setColor(mOuterBackground);
mOuterPaint.setStrokeWidth(mRoundWidth);
mOuterPaint.setStyle(Paint.Style.STROKE);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(mProgressTextColor);
mTextPaint.setTextSize(mProgressTextSize);
}
private int sp2px(float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
}
private float dip2px(int dip) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 只保证是正方形
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(Math.min(width, height), Math.min(width, height));
}
@Override
protected void onDraw(Canvas canvas) {
// 先画内圆
int center = getWidth() / 2;
canvas.drawCircle(center, center, center - mRoundWidth / 2, mInnerPaint);
// 画外圆,画圆弧
RectF rect = new RectF(0 + mRoundWidth / 2, 0 + mRoundWidth / 2,
getWidth() - mRoundWidth / 2, getHeight() - mRoundWidth / 2);
if (mProgress == 0) {
return;
}
float percent = (float) mProgress / mMax;
canvas.drawArc(rect, 0, percent * 360, false, mOuterPaint);
// 画进度文字
String text = ((int) (percent * 100)) + "%";
Rect textBounds = new Rect();
mTextPaint.getTextBounds(text, 0, text.length(), textBounds);
int x = getWidth() / 2 - textBounds.width() / 2;
Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
int baseLineY = getHeight() / 2 + dy;
canvas.drawText(text, x, baseLineY, mTextPaint);
}
// 给几个方法
public synchronized void setMax(int max) {
if (max < 0) {
// todo 待完善
}
this.mMax = max;
}
public synchronized void setProgress(int progress) {
if (progress < 0) {
}
this.mProgress = progress;
// 刷新 invalidate
invalidate();
}
}
Activity.java
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
mProgressBar.setMax(4000);
ValueAnimator animator = ObjectAnimator.ofFloat(0, 4000);
animator.setDuration(2000);
animator.start();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float progress = (float) animation.getAnimatedValue();
mProgressBar.setProgress((int) progress);
}
});
进度条应用很广泛,如加载、下载、启动页跳转等场景很多都需要,可以从此基础上进行修改和完善。