//自定义view类
private Context context;
private int progress = 0;
private Paint paint;
private int drowable;
private boolean flag = false;
private int x;
private int color;
private int color1;
private int r;
private int wX;
private int max;
public MySrcView(Context context) {
this(context, null);
}
public MySrcView(Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public MySrcView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
this.paint = new Paint();
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setAntiAlias(true);
initProsecc(context, attrs);
}
public synchronized void setProcress(int progress) {
if(progress<0){
progress=0;
}else if(progress>max){
progress=max;
}else{
this.progress = progress;
}
}
public int getMax() {
return max;
}
private void initProsecc(Context mContext, AttributeSet attrs) {
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.MySrcView);
color = typedArray.getColor(R.styleable.MySrcView_bgcorlor, Color.RED);
color1 = typedArray.getColor(R.styleable.MySrcView_fgcolor, Color.GREEN);
r = typedArray.getInt(R.styleable.MySrcView_r, 360);
wX = typedArray.getInt(R.styleable.MySrcView_width, 10);
max = typedArray.getInt(R.styleable.MySrcView_max, 100);
drowable = typedArray.getInt(R.styleable.MySrcView_drowStyle,0);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth() / 2; // 圆心位置
this.paint.setColor(color);
this.paint.setStrokeWidth(wX);
canvas.drawCircle(center, center, r, this.paint);
// 绘制圆环
this.paint.setColor(color1);
if(drowable==0){
this.paint.setStyle(Paint.Style.STROKE);
flag=false;
}else{
this.paint.setStyle(Paint.Style.FILL);
flag=true;
}
int top = (center - r);
int bottom = (center + r);
RectF oval = new RectF(top, top, bottom, bottom);
canvas.drawArc(oval, 270, 360*progress/max, flag, paint);
}
//mainactivity内写入
private MySrcView progressView;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
startActivity(new Intent(MainActivity.this,TwoActivity.class));
handler.sendEmptyMessageDelayed(0,10000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressView = (MySrcView) findViewById(R.id.MyCircleProgress);
new ProgressAnimation().execute();
handler.sendEmptyMessageDelayed(0,10000);
}
class ProgressAnimation extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
//进度值不断的变化
for (int i = 0; i < progressView.getMax(); i++) {
try {
publishProgress(i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
//更新进度值
// progressView.setProgress(values[0]);
progressView.setProcress(values[0]);
progressView.invalidate();
super.onProgressUpdate(values);
}
}