1、Android 圆环里绘制有多段不同颜色粗弧线、弧线为圆角弧线 2、圆环和弧线上面也有点 3、中心有文字
时间: 2023-07-10 16:08:40 浏览: 44
实现这个效果可以使用 Canvas 绘图技术。具体步骤如下:
1. 创建一个自定义 View,重写 onDraw 方法,获取 Canvas 对象。
2. 设置画笔的颜色和样式,设置为圆角弧线。
3. 使用 drawArc 方法绘制圆环。
4. 计算每段弧线的起始角度和结束角度,并使用 drawArc 方法绘制弧线。
5. 使用 drawText 方法绘制中心的文字。
6. 使用 drawCircle 方法绘制圆环和弧线上的点。
代码示例:
```java
public class CustomView extends View {
private Paint mPaint;
private RectF mRectF;
private String[] mTextArray = { "A", "B", "C", "D" };
private int[] mColorArray = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
private int[] mProgressArray = { 60, 90, 120, 90 };
private float mStartAngle = -90;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mRectF = new RectF(50, 50, 450, 450);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStrokeWidth(50);
mPaint.setColor(Color.GRAY);
canvas.drawArc(mRectF, 0, 360, false, mPaint);
for (int i = 0; i < mProgressArray.length; i++) {
mPaint.setColor(mColorArray[i]);
float sweepAngle = mProgressArray[i] * 1.0f / 360 * 360;
canvas.drawArc(mRectF, mStartAngle, sweepAngle, false, mPaint);
mStartAngle += sweepAngle;
}
mPaint.setStrokeWidth(10);
mPaint.setColor(Color.WHITE);
float x = mRectF.centerX();
float y = mRectF.centerY();
canvas.drawText("Hello World", x, y, mPaint);
mPaint.setStrokeWidth(20);
mPaint.setColor(Color.WHITE);
for (int i = 0; i < mProgressArray.length; i++) {
float sweepAngle = mProgressArray[i] * 1.0f / 360 * 360;
float angle = mStartAngle - sweepAngle / 2;
float cx = (float) (x + (mRectF.width() / 2 - 30) * Math.cos(angle * Math.PI / 180));
float cy = (float) (y + (mRectF.width() / 2 - 30) * Math.sin(angle * Math.PI / 180));
canvas.drawCircle(cx, cy, 10, mPaint);
}
}
}
```