每两秒获取一次坐标信息,每次绘制将距离最远的点的长度作为圆半径,实现步骤:
1.绘制坐标轴
绘制角度标注用到的方法:
canvas方法 :rotate旋转画布,save保存画布状态,restore恢复上次保存的状态;
private void drawAxis(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(25);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
Paint circle = new Paint();
circle.setColor(getResources().getColor(R.color.radarBackColor));
circle.setAntiAlias(true);
circle.setStyle(Paint.Style.FILL);
if (canvas != null) {
// 画直线,圆
width = (centerX > centerY ? centerY : centerX) * 17 / 20;//所画圆占17/20
canvas.drawCircle(centerX, centerY, width, circle);
canvas.drawCircle(centerX, centerY, width, paint);
Paint assistLine = new Paint();//辅助线画笔
assistLine.setColor(getResources().getColor(R.color.AssistLine));
assistLine.setAntiAlias(true);
assistLine.setStyle(Paint.Style.STROKE);
for (int i = 1; i < ASSISTCIRCLE; i++) {//圆形辅助线
canvas.drawCircle(centerX, centerY, width * i / ASSISTCIRCLE, assistLine);
}
for (int i = 0; i <= ASSISTPOINT; i++) {//距离辅助线
canvas.drawLine(centerX + width * i / ASSISTPOINT, centerY, centerX + width * i / ASSISTPOINT, centerY - width / 20, assistLine);
canvas.drawText(maxlength * i / ASSISTPOINT + "", centerX + width * i / ASSISTPOINT - width / 20, centerY + width / 20, paint);
}
for (int i = 0; i < ASSISTANGLE; i++) {//角度辅助线
canvas.save();//保存canvas状态
canvas.rotate(-360 / ASSISTANGLE * i + 90, centerX, centerY);
canvas.drawLine(centerX, centerY, centerX, centerY - width, assistLine);
if (i < ASSISTANGLE / 2 + 1) {
canvas.drawText(i * 360 / ASSISTANGLE + "°", centerX, centerY - width * 21 / 20, paint);
canvas.restore();//重置canvas状态
} else {
canvas.rotate(180, centerX, centerY);
Paint.FontMetrics fr = paint.getFontMetrics();
int lim = (int) Math.ceil(fr.descent - fr.top) + 2;//获取文本高度
canvas.drawText(i * 360 / ASSISTANGLE + "°", centerX, centerY + width * 21 / 20 + lim, paint);
canvas.restore();//重置canvas状态
}
}
//画横纵轴
canvas.drawLine(centerX - width, centerY, centerX + width, centerY, paint);
canvas.drawLine(centerX, centerY - width, centerX, centerY + width, paint);
}
}
2.设置一个传入坐标的方法
//设置点位
public void setPoints(ArrayList list) {
maxlength=50;//每次绘制重置最大半径
points = new ArrayList<>();
for (WiFiDistance distance :
list) {
if (maxlength < distance.distance) {
maxlength = distance.distance;
}
}
for (WiFiDistance distance :
list) {
int x = (int) (Math.cos(distance.angle * Math.PI / 180) * distance.distance * width / maxlength);
int y = (int) (Math.sin(distance.angle * Math.PI / 180) * distance.distance * width / maxlength);
points.add(new Point(x, y));
}
invalidate();//刷新view
}
invalidate 回调用ondraw ()重新绘制:在ondraw()方法中绘制所需点。
3.添加绘制点的单击事件(重写onTouchEcent方法)
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
onPointClickListener.onPointClickListen(getPoint(event.getX(), event.getY()));
break;
}
return true;
}
//判断按下的是哪个点
private int getPoint(float x, float y) {
x = x - centerX;
y = centerY - y;
if (points != null) {
for (int i = 0; i < points.size(); i++) {
if (Math.sqrt(Math.pow(points.get(i).x - x, 2) + Math.pow(points.get(i).y - y, 2)) <
RANG) {
return i;
}
}
}
return -1;//-1表示无对应点
}
监听事件实现, 建立一个监听j接口(OnPointClickListener),在RadarView中添加该类的成员变量,在ontouchevent()中调用;