```
/**
* Created by zhaoming on 2018/4/3.
*/
public class ZMProgressBarextends View {
private int height;
private int width;
private int width2;
private Rect rect;
private Paint paint;
private int color1 = Color.BLUE;
private int color2 = Color.CYAN;
private int percent =0;
private Runnable r;
private Handler handler;
private Rect rect2;
private RectF rectF;
private RectF rectF2;
public ZMProgressBar(Context context,@Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
paint =new Paint();
paint.setTextSize(80);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
start();
}
});
handler =new Handler();
r =new Runnable() {
@Override
public void run() {
if (percent <100) {
percent++;
width2 = (int) (width *percent /100);
start();
postInvalidate();
}else {
percent =0;
}
}
};
}
private void start() {
handler.postDelayed(r,30);
rectF2.set(0,0,width2,height);
}
@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width,height);
}
@Override
protected void onDraw(Canvas canvas) {
if (rect ==null) {
rect =new Rect(0,0,width,height);
rectF =new RectF(rect);
rect2 =new Rect(0,0,width2,height);
rectF2 =new RectF(rect2);
}
paint.setColor(color1);
canvas.drawRoundRect(rectF,50,50,paint);
paint.setColor(color2);
canvas.drawRoundRect(rectF2,50,50,paint);
paint.setColor(Color.RED);
canvas.drawText(percent +"%",width /2 -20,height /2 +10,paint);
}
}
```