如何在canvas
中为这些弧设置不同的半径?
PieView.java
public class PieView extends View {
private ArrayList<Float> divisionPoints = new ArrayList<>();
private float viewWidth;
private float viewHeight;
private float radius;
private Paint paint = new Paint();
private RectF rectF = new RectF();
private float startAngle = 90;
private RadialGradient radialGradientCyan, radialGradientBlue, radialGradientDarkBlue, radialGradientGreen;
private ArrayList<RadialGradient> radialGradients = new ArrayList<>();
public PieView(Context context) {
super(context);
}
public PieView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public PieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
viewWidth = r - l;
viewHeight = b - t;
}
public void setUp(final ArrayList<Float> points) {
this.postDelayed(new Runnable() {
@Override
public void run() {
divisionPoints.addAll(points);
if (viewHeight > viewWidth) {
radius = viewWidth / 2;
} else {
radius = viewHeight / 2;
}
setRectValues();
//edit the stop points to edit the gradients
float[] stopPoints = {.6f, .8f};
int[] blue = {Color.parseColor("#FF2D82AE"), Color.parseColor("#FF2D82AE")};
int[] darkBlue = {Color.parseColor("#FF2A79A4"), Color.parseColor("#FF2A79A4")};
int[] cyan = {Color.parseColor("#FF50C0CA"), Color.parseColor("#FF50C0CA")};
int[] green = {Color.parseColor("#FF9BDC90"), Color.parseColor("#FF9BDC90")};
radialGradientCyan = new RadialGradient(viewWidth / 2, viewHeight / 2, radius, cyan, stopPoints, Shader.TileMode.CLAMP);
radialGradientBlue = new RadialGradient(viewWidth / 2, viewHeight / 2, radius, blue, stopPoints, Shader.TileMode.CLAMP);
radialGradientDarkBlue = new RadialGradient(viewWidth / 2, viewHeight / 2, radius, darkBlue, stopPoints, Shader.TileMode.CLAMP);
radialGradientGreen = new RadialGradient(viewWidth / 2, viewHeight / 2, radius, green, stopPoints, Shader.TileMode.CLAMP);
radialGradients.add(radialGradientCyan);
radialGradients.add(radialGradientBlue);
radialGradients.add(radialGradientDarkBlue);
radialGradients.add(radialGradientGreen);
postInvalidate();
}
}, 0);
}
private void setRectValues() {
rectF.left = viewWidth / 2 - radius;
rectF.top = viewHeight / 2 - radius;
rectF.right = viewWidth / 2 + radius;
rectF.bottom = viewHeight / 2 + radius;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (divisionPoints != null) {
for (int i = 0; i < divisionPoints.size(); i++) {
float distance = (float) (divisionPoints.get(i) * 3.6);
paint.reset();
paint.setAntiAlias(true);
paint.setShader(radialGradients.get(i));
canvas.drawArc(rectF, startAngle, distance, true,paint);
startAngle += distance;
}
}
paint.reset();
}
}
如何根据PieActivity
中给出的饼图数据设置半径的小偏移而不使用任何库。
drawArc()
- 方法没有要绘制的radius参数。
我发现只有两个
drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)