自定义View代码如下:

public class CircleProgress extends View {

private Paint mPaint;
private int mWidth;
private int mHeight;
private static final int RING_WIDTH =80;

public CircleProgress(Context context) {
super(context);
initPaint();
}

private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.YELLOW);
}

public CircleProgress(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mWidth = getWidth();
mHeight = getHeight();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(RING_WIDTH);
canvas.drawCircle(mWidth / 2, mHeight / 2, 100, mPaint);
}
}

 

MainActivity.java代码如下:

 

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CircleProgress(this));
}
}

 

运行实例如下:

Anroid自定义View-绘制圆环_自定义view