<declare-styleable name="ProgressView">
<attr name="circleColor" format="color|reference"></attr>
<attr name="sweepColor" format="color|reference"></attr>
<attr name="startAngle" format="integer"></attr>
<attr name="sweepStep" format="integer"></attr>
<attr name="padding" format="integer"></attr>
</declare-styleable>
public class ProgressView extends View {
private int sweepStep = 10;
private int padding = 40;
private int circleColor = Color.GRAY;
private int sweepColor = Color.BLUE;
private int startAngle = 90;
private int storke = 20;
private int sweepAngle = 0;
private static final int DEFAULT_WIDTH = 200;
private static final int DEFAULT_HEIGHT = 200;
private Paint mPaint;
private Canvas canvas1;
public ProgressView(Context context) {
super(context);
}
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressView);
if (array != null) {
sweepStep = array.getInteger(R.styleable.ProgressView_sweepStep, sweepStep);
padding = array.getInteger(R.styleable.ProgressView_padding, padding);
circleColor = array.getColor(R.styleable.ProgressView_circleColor, circleColor);
sweepColor = array.getColor(R.styleable.ProgressView_sweepColor, sweepColor);
startAngle = array.getInteger(R.styleable.ProgressView_startAngle, startAngle);
array.recycle();
}
}
/**
* 先绘制外边框 --内部扇形
*
* @param canvas
*/
int one = 260;
int two = 260;
int three = 260;
int fore =260;
@Override
protected void onDraw(Canvas canvas) {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(circleColor);
mPaint.setStrokeWidth(storke);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2 - storke / 2, mPaint);
invalidate();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(sweepColor);
RectF rectF1 = new RectF(one,two,three,fore);
canvas.drawArc(rectF1, 360f, 360f, true, mPaint);
one = one -2;
two = two - 2;
three=three+2;
fore=fore+2;
one = one<(padding + storke)?(padding + storke):one;
two = two<(padding + storke)?(padding + storke):two;
three = three>(getWidth() - padding - storke)?(getWidth() - padding - storke):three;
fore = fore>(getWidth() - padding - storke)?(getWidth() - padding - storke):fore;
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
int wSize = MeasureSpec.getSize(widthMeasureSpec);
int hSize = MeasureSpec.getSize(heightMeasureSpec);
switch (wMode) {
case MeasureSpec.AT_MOST:
float density = getResources().getDisplayMetrics().density;
wSize = (int) (DEFAULT_WIDTH * density);
hSize = (int) (DEFAULT_HEIGHT * density);
break;
case MeasureSpec.EXACTLY:
wSize = hSize = Math.min(wSize, hSize);
break;
}
setMeasuredDimension(wSize, hSize);
}
public void initData(){
postInvalidate();
one = 260;
two = 260;
three = 260;
fore =260;
}
}
public class MainActivity extends AppCompatActivity {
private ProgressView pv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pv = (ProgressView) findViewById(R.id.pv);
}
public void dy(View v) {
new Thread(new Runnable() {
@Override
public void run() {
pv.initData();
}
}).start();
}
}