public class MyView extends View {
private Paint paint;
private int roundColor, roundProgressColor;
private float roundWidth;
private int style;
private int process = 1;
private Canvas mcanvas;
private RectF oval;
public MyView(Context context) {
this(context, null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
TypedArray mtypeArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
roundColor = mtypeArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
roundWidth = mtypeArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 15);
roundProgressColor = mtypeArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
style = mtypeArray.getInt(R.styleable.RoundProgressBar_style, 0);
mtypeArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth() / 2;
int radius = (int) (center - roundWidth / 2);
//
// paint.setColor(roundColor);
// paint.setStyle(Paint.Style.STROKE);
// paint.setStrokeWidth(roundWidth);
// paint.setAntiAlias(true);
// PathEffect effects = new DashPathEffect(new float[]{10,10,10,10},1);
// paint.setPathEffect(effects);
// canvas.drawCircle(center, center, radius, paint);
paint.setStrokeWidth(roundWidth);
paint.setColor(roundProgressColor);
oval = new RectF(center - radius, center - radius, center + radius, center + radius);
paint.setStyle(Paint.Style.STROKE);
PathEffect effects = new DashPathEffect(new float[]{10, 10, 10, 10}, 1);
paint.setAntiAlias(true);
paint.setPathEffect(effects);
canvas.drawArc(oval, 120, 30*process%300, false, paint);
mcanvas = canvas;
Log.i("pan", "meiyoujinlai");
Log.i("pan", canvas.toString() + "meiyou");
new Thread() {
@Override
public void run() {
super.run();
Message msg = new Message();
msg.what = 1;
while (true) {
try {
mHandler.sendMessage(msg);
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
Log.i("pan", "xianchengzaifa!!");
Log.i("pan", mcanvas + "is value");
mcanvas.drawArc(oval, 120, process % 300, false, paint);
process += 20;
}
}
};
}
上面是我的代码,下面是我的效果图。这是我想实现的动态效果: