绘制步骤
1.首先创建以JAVA类继承与View,然后调用前两个构造器
2.然后设置画布的宽度和高度
3.绘制图形(见代码)
4.在XML文件布局中添加该View并附上id
5.为了模仿下载进度条,需要在Activity中找到该View,利用Handler提交数据即提交下载完成度,并不断更新View(可以设置延迟来模仿)
6.代码实现
弧形进度条
自定义View
public class ProgressArcView extends View {
private int width;
private int heigth;
private int progressCurrent;
private int progressMax=100;
private Paint mPaintMax;
private Paint mPaintCurrent;
private Paint mPaintText;
public void setProgressCurrent(int progressCurrent) {
this.progressCurrent = progressCurrent;
invalidate();
}
public ProgressArcView(Context context) {
super(context);
}
public ProgressArcView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintMax=new Paint();
mPaintMax.setAntiAlias(true);
mPaintMax.setColor(Color.GRAY);
mPaintMax.setStyle(Paint.Style.STROKE);
mPaintMax.setStrokeWidth(100);
mPaintCurrent=new Paint();
mPaintCurrent.setAntiAlias(true);
mPaintCurrent.setColor(Color.BLUE);
mPaintCurrent.setStyle(Paint.Style.STROKE);
mPaintCurrent.setStrokeWidth(100);
mPaintText=new Paint();
mPaintText.setAntiAlias(true);
mPaintText.setColor(Color.BLACK);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setTextSize(80);
}
//设置画布的宽度和高度
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width=getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
heigth=getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width,heigth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width/2,heigth/2,300</