资源说明:package com.example.task;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CircleProgressBar extends View {
private int maxProgress = 100;
private int progress = 10;
private int progressStrokeWidth = 20;
// 画圆所在的距形区域
RectF oval;
Paint paint;
public CircleProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO 自动生成的构造函数存根
oval = new RectF();
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO 自动生成的方法存根
super.onDraw(canvas);
int width = this.getWidth();
int height = this.getHeight();
if (width != height) {
int min = Math.min(width, height);
width = min;
height = min;
}
paint.setAntiAlias(true); // 设置画笔为抗锯齿
paint.setColor(0xFFE2E2E2); // 设置画笔颜色
canvas.drawColor(Color.TRANSPARENT); // 白色背景
paint.setStrokeWidth(progressStrokeWidth); // 线宽
paint.setStyle(Style.STROKE);
oval.left = progressStrokeWidth / 2; // 左上角x
oval.top = progressStrokeWidth / 2; // 左上角y
oval.right = width - progressStrokeWidth / 2; // 左下角x
oval.bottom = height - progressStrokeWidth / 2; // 右下角y
canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景
// paint.setColor(Color.rgb(0x57, 0x87, 0xb6));
paint.setColor(0xFFFF4700);
canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360,
false, paint); // 绘制进度圆弧,这里是蓝色
paint.setStrokeWidth(1);
String text = progress + "分";
int textHeight = height / 4;
paint.setTextSize(textHeight);
int textWidth = (int) paint.measureText(text, 0, text.length());
paint.setStyle(Style.FILL);
// canvas.drawText(text, width / 2 - textWidth / 2, height / 2
// + textHeight / 2, paint);
}
public int getMaxProgress() {
return maxProgress;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
public void setProgress(int progress) {
this.progress = progress;
this.invalidate();
}
/**
* 非UI线程调用
*/
public void setProgressNotInUiThread(int progress) {
this.progress = progress;
this.postInvalidate();
}
}
上面的代码就是画两个圆弧
package com.example.task;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
CircleProgressBar completedView1;
CircleProgressBar completedView2;
CircleProgressBar completedView3;
CircleProgressBar completedView4;
CircleProgressBar completedView5;
CircleProgressBar completedView6;
CircleProgressBar completedView7;
CircleProgressBar completedView8;
CircleProgressBar completedView9;
CircleProgressBar completedView10;
CircleProgressBar completedView11;
CircleProgressBar completedView12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
completedView1 = (CircleProgressBar) findViewById(R.id.circleProgressBar1);
completedView2 = (CircleProgressBar) findViewById(R.id.circleProgressBar2);
completedView3 = (CircleProgressBar) findViewById(R.id.circleProgressBar3);
completedView4 = (CircleProgressBar) findViewById(R.id.circleProgressBar4);
completedView5 = (CircleProgressBar) findViewById(R.id.circleProgressBar5);
completedView6 = (CircleProgressBar) findViewById(R.id.circleProgressBar6);
completedView7 = (CircleProgressBar) findViewById(R.id.circleProgressBar7);
completedView8 = (CircleProgressBar) findViewById(R.id.circleProgressBar8);
completedView9 = (CircleProgressBar) findViewById(R.id.circleProgressBar9);
completedView10 = (CircleProgressBar) findViewById(R.id.circleProgressBar10);
completedView11 = (CircleProgressBar) findViewById(R.id.circleProgressBar11);
completedView12 = (CircleProgressBar) findViewById(R.id.circleProgressBar12);
this.findViewById(R.id.button_show).setOnClickListener(
new OnClickListener() {
private int i2 = 40;
@Override
public void onClick(View v) {
// completedView1.setProgressNotInUiThread(i2 % 100);
// completedView2.setProgressNotInUiThread(i2 % 100);
// completedView3.setProgressNotInUiThread(i2 % 100);
// completedView4.setProgressNotInUiThread(i2 % 100);
// completedView5.setProgressNotInUiThread(i2 % 100);
// completedView6.setProgressNotInUiThread(i2 % 100);
// completedView7.setProgressNotInUiThread(i2 % 100);
// completedView8.setProgressNotInUiThread(i2 % 100);
// completedView9.setProgressNotInUiThread(i2 % 100);
// completedView10.setProgressNotInUiThread(i2 % 100);
// completedView11.setProgressNotInUiThread(i2 % 100);
// completedView12.setProgressNotInUiThread(i2 % 100);
// i2 += 10;
new Thread() {
public void run() {
int i = 0;
while (i <= i2) {
completedView1.setProgressNotInUiThread(i);
completedView2.setProgressNotInUiThread(i);
completedView3.setProgressNotInUiThread(i);
completedView4.setProgressNotInUiThread(i);
completedView5.setProgressNotInUiThread(i);
completedView6.setProgressNotInUiThread(i);
completedView7.setProgressNotInUiThread(i);
completedView8.setProgressNotInUiThread(i);
completedView9.setProgressNotInUiThread(i);
completedView10.setProgressNotInUiThread(i);
completedView11.setProgressNotInUiThread(i);
completedView12.setProgressNotInUiThread(i);
i++;
try {
sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}.start();
i2 += 10;
}
});
}
}
上面的主活动界面使用线程画图
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。