需求:
1. 将一个圆形6等分的饼图,在每个饼图的中心显示当前饼图代表的含义
绘制步骤:
1. 画布中心点: centerX = getWidht() / 2; centerY = getWidth() / 2;
2. 原型的半径, 定义为高度的4分之一, radius = getHeight() / 4 > getWidth() /2 ? getWidth() /2 : getHeight() /4;
3. 由于绘制弧度图时是从相对Y轴90度开始的,定义开始角度为 startAngle, 绘制的角度为 sweepAngle, 弧度图中心点的位置用来显示数字,则其中心点相对于Y轴角度为 textAngle = 90 + startAngle + sweepAngle / 2;
4. 计算绘制文本的坐标, textX = centerX + radius * Math.sin(Math.toRadians(textAngle);
textY = centerY - radius * Math.sin(Math.toRadians(textAngle);
具体代码片段如下:
private float[] calculatePosition(float centerX, float centerY, float radius, float degree) {
//由于Math.sin(double a)中参数a不是度数而是弧度,所以需要将度数转化为弧度
//而Math.toRadians(degree)的作用就是将度数转化为弧度
//sin 一二正,三四负 sin(180-a)