如下该类实现了两边半圆形状进度条:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class SemiCircleDrawable extends Drawable {
private Paint paint;
private int arcWidth;
private int color;

public SemiCircleDrawable(int arcWidth, int color) {
this.arcWidth = arcWidth;
this.color = color;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setStyle(Paint.Style.FILL);
}

@Override
public void draw(@NonNull Canvas canvas) {
Rect bounds = getBounds();
float right = bounds.right;
float left = right - arcWidth;
float top = bounds.top;
float bottom = bounds.bottom;

// 画左半圆
canvas.drawArc(new RectF(left, top, right, bottom), -90, 180, true, paint);
}

@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}

@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
paint.setColorFilter(colorFilter);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

@Override
public int getIntrinsicWidth() {
return arcWidth;
}

@Override
public int getIntrinsicHeight() {
return arcWidth;
}
}

可以采用如下代码来使用该类:

1
2
3
4
5
SemiCircleDrawable leftDrawable = new SemiCircleDrawable(50, Color.BLUE); // 半圆的宽度为50px,颜色为蓝色
SemiCircleDrawable rightDrawable = new SemiCircleDrawable(50, Color.RED); // 半圆的宽度为50px,颜色为红色

ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setProgressDrawable(new LayerDrawable(new Drawable[]{leftDrawable, rightDrawable}));