因为刚刚学习了Android画布操作,为了巩固一下,写了一个自定义雷达控件这个控件只暴露了一个一个最基本的接口,设置数据,对坐标轴数量,单位长度等统计要素都没考虑,如果以后有时间可能会添加进去。先上个截图
上个核心代码
packagehuiyan.com.costomview_learn2;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Path;
importandroid.support.annotation.Nullable;
importandroid.util.AttributeSet;
importandroid.view.View;
importjava.util.ArrayList;
/**
* Created by lenovo on 2017/7/5.
*/
publicclassMyViewextendsView{
privatePaintmPaint;//绘制放射线的画笔
privatePainthexPaint;//绘制六边形的画笔
privatePaintpoPaint;//绘制数据的画笔
privateArrayListdatas=newArrayList<>();
publicMyView(Contextcontext) {
super(context);
init();
}
publicMyView(Contextcontext, @NullableAttributeSetattrs) {
super(context,attrs);
init();
}
publicMyView(Contextcontext, @NullableAttributeSetattrs,intdefStyleAttr) {
super(context,attrs,defStyleAttr);
init();
}
publicMyView(Contextcontext, @NullableAttributeSetattrs,intdefStyleAttr,intdefStyleRes) {
super(context,attrs,defStyleAttr,defStyleRes);
init();
}
publicvoidinit() {
/*
*对画笔进行初始化,进行各种设置
*/
mPaint=newPaint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.BLUE);
hexPaint=newPaint();
hexPaint.setStyle(Paint.Style.STROKE);
hexPaint.setColor(Color.GRAY);
poPaint=newPaint();
poPaint.setStyle(Paint.Style.FILL);
poPaint.setColor(getResources().getColor(R.color.red));
poPaint.setStrokeWidth(5);
poPaint.setAlpha(100);
}
@Override
protectedvoidonDraw(Canvascanvas) {
/*
*画图
*/
floatwidth=getWidth();
floatheight=getHeight();
super.onDraw(canvas);
datas.add((float)300);
datas.add((float)300);
datas.add((float)300);
datas.add((float)300);
datas.add((float)300);
datas.add((float)300);
canvas.translate(width/2,height/2);
PathLpath=newPath();
drawRad(Lpath,canvas,mPaint);
canvas.rotate(-120);//坐标系复位
canvas.scale(1,-1);//坐标系复位
//drawHex(canvas, hexPaint, 300);
for(inti=0;i<7;i++) {
drawHex(canvas,hexPaint,i*50);
canvas.rotate(-30);//坐标系复位
canvas.translate(-i*50,0);//坐标系复位
canvas.rotate(60);//坐标系复位
}
for(inti=0;i<5;i++) {
canvas.drawLine((float) (datas.get(i)*Math.cos(i*Math.PI/3)), (float) (datas.get(i)*Math.sin(i*Math.PI/3))
, (float) (datas.get(i+1)*Math.cos((i+1)*Math.PI/3)), (float) (datas.get(i+1)*Math.sin((i+1)*Math.PI/3)),poPaint);
}
canvas.drawLine((float) (datas.get(5)*Math.cos(-Math.PI/3)), (float) (datas.get(5)*Math.sin(-Math.PI/3))
, (float) (datas.get(0)*Math.cos(6*Math.PI/3)), (float) (datas.get(0)*Math.sin(6*Math.PI/3)),poPaint);
}
publicvoidsetDatas(ArrayListdatas) {
/*
*设置数据进行重绘
*/
this.datas=datas;
invalidate();
}
privatevoiddrawHex(Canvascanvas,Paintpaint,intx) {
canvas.translate(x,0);
canvas.rotate(30);
canvas.drawLine(0,0,0,x,paint);
for(inti=0;i<5;i++) {
canvas.translate(0,x);
canvas.rotate(60);
canvas.drawLine(0,0,0,x,paint);
}
}
privatevoiddrawRad(Pathpath,Canvascanvas,Paintpaint) {
path.moveTo(-300,0);
path.lineTo(300,0);
canvas.drawPath(path,paint);
canvas.rotate(60);
canvas.drawPath(path,paint);
canvas.rotate(60);
canvas.drawPath(path,paint);
}
}