效果图
代码如下:
public class CircleView extends View {
Paint paint;
Context context;
//构造方法
public CircleView(Context context) {
super(context);
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CircleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context=context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
/*绘图*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//得到屏幕宽高
int width = getWidth();
int radius = width - 450/2;
int height = getHeight();
// 创建画笔
Paint paint1 = new Paint();
Paint paint2 = new Paint();
// 消除锯齿
paint1.setAntiAlias(true);
paint2.setAntiAlias(true);
//画笔颜色
paint1.setColor(Color.GRAY);
paint2.setColor(Color.WHITE);
//设置圆环形状和大小
RectF oval = new RectF(width-radius,width-radius,width+radius,width+radius);
paint1.setStrokeWidth(450);
canvas.drawArc(oval,-90,90,false,paint1);
canvas.drawCircle(width/2,90,90,paint1);
canvas.drawCircle(width/2,90,80,paint2);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
}
直线 public void drawLine (float startX, float startY, float stopX, float stopY, Paint paint)
startX:起始端点的X坐标。
startY:起始端点的Y坐标。
stopX:终止端点的X坐标。
stopY:终止端点的Y坐标。
paint:绘制直线所使用的画笔。
paint.setColor(Color.BLACK); //设置画笔颜色
canvas.drawColor(Color.WHITE); //设置背景颜色
paint.setStrokeWidth((float) 1.0); //设置线宽
canvas.drawLine(50, 50, 450, 50, paint); //绘制直线
- 代码实现
public class NavBarBackgroundView extends View {
private Paint mPaint = new Paint();
public NavBarBackgroundView(Context context) {
this(context,null);
}
public NavBarBackgroundView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint.setColor(Color.parseColor("#68D8F9"));
mPaint.setStrokeWidth((float) 3.0); //设置线宽
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//得到屏幕宽高
int width = getWidth();
int radius = width - 450/2;
int height = getHeight();
// 创建画笔
Paint paint1 = new Paint();
// 消除锯齿
paint1.setAntiAlias(true);
//画笔颜色
paint1.setColor(Color.parseColor("#68D8F9"));
//设置圆环形状和大小
RectF oval = new RectF(width-radius,width-radius,width+radius,width+radius);
paint1.setStrokeWidth(450);
canvas.drawArc(oval,-90,90,false,paint1);
canvas.drawCircle(width-5,5,5,paint1);//右面圆点
canvas.drawCircle(5,5,5,paint1);//左面圆点
canvas.drawLine(0,0,width,0,mPaint);//横线
canvas.drawLine(0,0,0,100,mPaint);//左面竖线
canvas.drawLine(width,0,width,50,mPaint);//右面竖线
}
}