要实现Android环形图案有中央竖线,可以使用自定义View来绘制。以下是一个示例代码:
public class CircularPatternView extends View {
private Paint paint;
private int centerX, centerY;
private int radius;
private int lineWidth;
public CircularPatternView(Context context) {
super(context);
init();
}
public CircularPatternView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CircularPatternView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setStrokeWidth(lineWidth);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2;
centerY = h / 2;
radius = Math.min(w, h) / 2;
lineWidth = 10; // 设置中央竖线的宽度
paint.setStrokeWidth(lineWidth);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制圆环
canvas.drawCircle(centerX, centerY, radius, paint);
// 绘制中央竖线
canvas.drawLine(centerX, centerY - radius, centerX, centerY + radius, paint);
}
}
要在布局文件中使用这个自定义View,可以添加如下代码:
这样就可以在Android环形图案中间绘制一个有中央竖线的圆环了。