android自定义进度圆与定时任务

时间:2022-01-21 07:46:25

先看代码:自定进度圆

public class ProgressCircle extends View {

private Paint paint;
private int strokewidth = 4;
private int alpha = 50;
private int progress = 30;
private Paint mpaint;

public ProgressCircle(Context context) {
super(context,null);
}

public ProgressCircle(Context context, AttributeSet attrs) {
super(context, attrs);

paint = new Paint();
paint.setColor(Color.BLACK);
paint.setDither(true);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(strokewidth);
paint.setStyle(Paint.Style.STROKE);
paint.setAlpha(alpha);

mpaint = new Paint();
mpaint.setColor(Color.RED);
mpaint.setDither(true);
mpaint.setAntiAlias(true);
mpaint.setStrokeCap(Paint.Cap.ROUND);
mpaint.setStrokeJoin(Paint.Join.ROUND);
mpaint.setStrokeWidth(strokewidth);
mpaint.setStyle(Paint.Style.STROKE);

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

int cx = getWidth()/2;
int cy = getHeight()/2;
int radius = (cx>cy?cy:cx)-strokewidth;

canvas.drawCircle(cx,cy,radius,paint);

RectF oval = new RectF(cx-radius,cy-radius,cx+radius,cy+radius);
canvas.drawArc(oval,90,(int)(-(3.6)*progress),false,mpaint);
}


public void setprogress( int progresss){
this.progress = progresss;

postInvalidate();//这个很重要,不能是Invalidate();

}
}

如果是invalidate();则会报错:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:853)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)

因为:invalidate是在UI线程中使用,而postInvalidate是在非UI线程中使用的,UI线程中不能做耗时任务;

看MainActivity代码:

public class MainActivity extends AppCompatActivity {

private ProgressCircle circle;
private Button load;
private int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

circle = (ProgressCircle) findViewById(R.id.circle);
load = (Button) findViewById(R.id.load);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(myrunable).start();
}
});
}

Runnable myrunable = new Runnable() {
@Override
public void run() {
// setdata();

Timedata();
}
};

private void setdata(){
for (int j = 0; j < 101; j++) {
circle.setprogress(j);
Log.e("tag", "run: "+j );
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private void Timedata(){
Timer timer = new Timer();
final TimerTask task = new TimerTask() {
@Override
public void run() {
circle.setprogress(i);
Log.e("tag", "run: "+i);
if (i>99){
cancel();
}
i++;
}
};
timer.schedule(task,10,100);

}

@Override
protected void onDestroy() {
super.onDestroy();


}
}

schedu的参数:timetask是指定时任务,第二个参数是指在在什么事件段内完成,我这里写的是10;第三个peroid是指在延时多少时间执行任务。

demo下载地址:https://github.com/renjiemei1225/ProgressCircle_12_7.git