自定义View练习 - 雷达信用图表
练习来源
练习的github:https://github.com/alidili/SesameCreditScore
最终效果图
思路
- 绘制多边形
- 绘制连接线
- 绘制填充区域
- 绘制总分数
- 绘制标题和图标
实现过程
0. 定义变量
//数据的数量
private int dataCount;
//弧度
private float radian;
//多边形的外接圆的半径
private int radius;
//中心点x
private int centerX;
//中心点y
private int centerY;
private List<CreditBean> creditBeans;
//最大分值
public int maxScore;
private Paint linePaint;
private Paint regionPaint;
private Paint totalScorePaint;
private Paint titlePaint;
1. 计算点坐标
- 计算坐标的示意图
/**
* 获得点坐标
*/
public Point getPoint(int index) {
return getPoint(index, 1);
}
/**
* 获得点坐标,percent为百分比,用于获得填充区域和文字等的坐标位置.
* 坐标的计算是重难点. 可以画图分析.
*/
public Point getPoint(int index, float percent) {
int x = 0;
int y = 0;
if (index == 0) {
x = (int) (centerX + radius * Math.sin(radian) * percent);
y = (int) (centerY - radius * Math.cos(radian) * percent);
} else if (index == 1) {
x = (int) (c