自定义雷达图

雷达图修改自网络https://github.com/jutao/AndroidNode/blob/master/2017/2017.3

效果图如下:

1

使用方法举例:

List datas =new ArrayList<>();

datas.add(new RadarView.ItemInfo("11",0));

datas.add(new RadarView.ItemInfo("22",0));

datas.add(new RadarView.ItemInfo("33",0));

datas.add(new RadarView.ItemInfo("44",0));

datas.add(new RadarView.ItemInfo("55",0));

viewHolder.myRadarview.setMaxValue(5);

viewHolder.myRadarview.setDatas(datas);

if (mTopList.size() !=0) {

viewHolder.myRadarview.updateDatas(mTopList);

}

以下为自定义RadarView

public class RadarView extends View {

private ContextmContext;

private static final StringTAG ="RadarView";

private Listdatas;

private float angle;

private float radius;

private float maxRadius;//网格最大半径

    private int centerX;//中心X

    private int centerY;//中心Y

    private float maxValue =100;//数据最大值

    private PaintmainPaint;//雷达区画笔

    private PaintvaluePaint;//数据区画笔

    private PainttextPaint;//文本画笔

    private PaintcirclePaint;//圆形画笔

    private int mainColor = Color.parseColor("#FD8BBA");

private int circleColor = Color.parseColor("#33ffffff");

private int defaultColor = Color.WHITE;

public void setDatas(List datas) {

this.datas = datas;

angle = (float) (Math.PI *2 / datas.size());

postInvalidate();

}

public void updateDatas(List datas) {

this.datas = datas;

angle = (float) (Math.PI *2 / datas.size());

postInvalidate();

}

/**

* 设置雷达图非白色的颜色

* main_color 雷达框框颜色

* circle_color 文字后面的圆形背景颜色

* default_color 雷达框内绘制区和绘制区圆点颜色

*/

    public void setBgColor(int main_color,int circle_color,int default_color) {

this.mainColor = main_color;

this.circleColor = circle_color;

this.defaultColor = default_color;

mainPaint.setColor(mainColor);

valuePaint.setColor(defaultColor);

circlePaint.setColor(circleColor);

}

public static class ItemInfo {

private Stringtitle;

private int value;

public ItemInfo(String title,int value) {

this.title = title;

this.value = value;

}

}

public RadarView(Context context) {

this(context,null,0);

}

public RadarView(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public RadarView(Context context, AttributeSet attrs,int defStyleAttr) {

super(context, attrs, defStyleAttr);

mContext = context;

init();

}

private void init() {

mainPaint =new Paint();//雷达区画笔

        mainPaint.setAntiAlias(true);

mainPaint.setStrokeWidth(ViewUtils.dip2px(mContext,1));

mainPaint.setColor(mainColor);

mainPaint.setStyle(Paint.Style.STROKE);

valuePaint =new Paint();//数据区画笔

        valuePaint.setAntiAlias(true);

valuePaint.setColor(defaultColor);

valuePaint.setStyle(Paint.Style.FILL);

textPaint =new Paint();//文本画笔

        textPaint.setAntiAlias(true);

textPaint.setColor(Color.WHITE);

datas =new ArrayList<>();

angle = (float) (Math.PI *2 /datas.size());//每个角的弧度

        circlePaint =new Paint();//圆形画笔

        circlePaint.setAntiAlias(true);

circlePaint.setStyle(Paint.Style.FILL);

circlePaint.setColor(circleColor);

}

@Override

    protected void onSizeChanged(int w,int h,int oldw,int oldh) {

radius = Math.min(h, w) /2 *0.34f;//雷达图半径

        maxRadius =radius / (datas.size() -1) *datas.size();

//中心坐标

        centerX = w /2;

centerY = h /2;

postInvalidate();

super.onSizeChanged(w, h, oldw, oldh);

}

@Override

    protected void onDraw(Canvas canvas) {

drawLines(canvas);

drawText(canvas);

drawRegion(canvas);

}

@Override

    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int size =0;

int width = getMeasuredWidth();

int height = getMeasuredHeight();

if (width > height) {

size = height;

}else {

size = width;

}

setMeasuredDimension(size, size);

//        setMeasuredDimension(ViewUtils.getScreenWidth(mContext) / 375 * 267,ViewUtils.getScreenWidth(mContext) / 375 * 267);

    }

/**

* 绘制直线

*/

    private void drawLines(Canvas canvas) {

Path path =new Path();

Path pathOut =new Path();

for (int i =0; i

path.reset();

path.moveTo(centerX,centerY);

float x = (float) (centerX +maxRadius * Math.sin(angle * i));

float y = (float) (centerY -maxRadius * Math.cos(angle * i));

path.lineTo(x, y);

if (i ==0)

pathOut.moveTo(x, y);

else

                pathOut.lineTo(x, y);

canvas.drawPath(path,mainPaint);//画出中间分割线

        }

//画出最外层多边形

        pathOut.close();

canvas.drawPath(pathOut,mainPaint);

}

/**

* 绘制文字

* 其实四个象限,是以(centerX, centerY)为圆点的象限

*

    * @param canvas

    */

    private void drawText(Canvas canvas) {

for (int i =0; i

float rx = (float) (centerX +maxRadius *1.77 * Math.sin(angle * i));

float ry = (float) (centerY -maxRadius *1.77 * Math.cos(angle * i));

drawCircle(canvas, rx, ry);//画出文字外边圈

            textPaint.setTextAlign(Paint.Align.CENTER);

textPaint.setTextSize(ViewUtils.sp2px(getContext(),18));

canvas.drawText(String.valueOf(datas.get(i ).value), rx, ry -9,textPaint);

textPaint.setTextSize(ViewUtils.sp2px(getContext(),12));

canvas.drawText(String.valueOf(datas.get(i ).title), rx, ry +40,textPaint);

}

}

/*

*  绘制文字外的圆

*/

    private void drawCircle(Canvas canvas,float x,float y) {

canvas.drawCircle(x, y, ViewUtils.dip2px(mContext,31.5f),circlePaint);

}

/**

* 绘制中间有效区域

*

    * @param canvas

    */

    private void drawRegion(Canvas canvas) {

Path path =new Path();

for (int i =0; i

double percent =datas.get(i).value /maxValue;//计算该数值与最大值比例

//小圆点所在位置距离圆心的距离

            float x = (float) (centerX +maxRadius * Math.sin(angle * i) * percent);

float y = (float) (centerY -maxRadius * Math.cos(angle * i) * percent);

if (i ==0) {

path.moveTo(x, y);

}else {

path.lineTo(x, y);

}

valuePaint.setStyle(Paint.Style.FILL);

canvas.drawCircle(x, y, ViewUtils.dip2px(mContext,2),valuePaint);

}

path.close();

canvas.save();

valuePaint.setColor(defaultColor);

valuePaint.setStyle(Paint.Style.FILL);

valuePaint.setAlpha(90);

canvas.drawPath(path,valuePaint);

canvas.save();

valuePaint.setStrokeWidth(ViewUtils.dip2px(mContext,1));

valuePaint.setColor(defaultColor);

valuePaint.setStyle(Paint.Style.STROKE);

canvas.drawPath(path,valuePaint);

}

public float getMaxValue() {

return maxValue;

}

//设置最大数值

    public void setMaxValue(float maxValue) {

this.maxValue = maxValue;

}

//设置蜘蛛网颜色

    public void setMainPaintColor(int color) {

mainPaint.setColor(color);

}

//设置标题颜色

    public void setTextPaintColor(int color) {

textPaint.setColor(color);

}

//设置覆盖局域颜色

    public void setValuePaintColor(int color) {

valuePaint.setColor(color);

}

}

你可能感兴趣的:(自定义雷达图)