效果如下:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#000000"
tools:context=".MainActivity">
<myapp.customview.CustomView.RadarView
android:id="@+id/radar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
RadarView.java
public class RadarView extends View {
private int CenterX = 0;
private int CenterY = 0;
private int radius1 = 0;
private int radius2 = 0;
private int space = 200;
private int grid_size_width = 5;
private int grid_size_height = 9;
private float CenterImageAngle = 0f;
private float LineAngle = 0f;
private boolean run = true;
private boolean start = false;
private int[] colors = new int[]{Color.parseColor("#00FF00"),Color.parseColor("#00FF33"),Color.parseColor("#00CC00")
,Color.parseColor("#00CC33"),Color.parseColor("#009900"),Color.parseColor("#009933")};
private int Color_Index = 0;
public RadarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mRunnable runnable = new mRunnable();
Thread thread = new Thread(runnable);
thread.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = this.getMeasuredWidth();
int height = this.getMeasuredHeight();
CenterX = width / 2;
CenterY = height / 2;
/*
背景色 黑色
*/
canvas.drawColor(Color.BLACK);
/*
绘制网格 grid
*/
Paint gridPaint = new Paint();
gridPaint.setStrokeWidth(3);
gridPaint.setColor(Color.DKGRAY);
int space = height / grid_size_height;
ArrayList<Float> linesList = new ArrayList<>();
for(int i = 1;i < grid_size_height;i++)
{
linesList.add(0f);
linesList.add((float) (space * i));
linesList.add((float) width);
linesList.add((float) (space * i));
}
int space2 = width / grid_size_width;
for(int i = 1;i < grid_size_width;i++)
{
linesList.add((float) (space2 * i));
linesList.add(0f);
linesList.add((float) (space2 * i));
linesList.add((float)height);
}
int len = linesList.size();
float[] lines = new float[len];
for(int i = 0;i < len;i++)
{
lines[i] = linesList.get(i);
}
canvas.drawLines(lines,gridPaint);
/*
绘制雷达波
*/
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawCircle(CenterX,CenterY,radius1,paint);
canvas.drawCircle(CenterX,CenterY,radius2,paint);
radius1 = radius1 + 5;
if(radius1 > width / 2)
{
start = true;
}
if(start)
{
radius2 = radius2 + 5;
}
if(radius1 > width && radius1 > height)
{
radius1 = 0;
}
if(radius2 > width && radius2 > height)
{
radius2 = 0;
}
/*
绘制中心人物
*/
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.center);
Matrix matrix = new Matrix();
matrix.postRotate(CenterImageAngle,bitmap.getWidth() / 2,bitmap.getWidth() / 2);
matrix.postTranslate(CenterX - bitmap.getWidth() / 2,CenterY - bitmap.getHeight() / 2);
canvas.drawBitmap(bitmap,matrix,new Paint());
/*
绘制目标点
*/
Paint mpaint = new Paint();
mpaint.setColor(colors[Color_Index]);
mpaint.setStyle(Paint.Style.FILL);
canvas.drawCircle(100,100,20,mpaint);
invalidate();
}
private class mRunnable implements Runnable
{
@Override
public void run() {
while(run)
{
CenterImageAngle = CenterImageAngle + 5;
if(CenterImageAngle > 360)
CenterImageAngle = 0;
Color_Index++;
if(Color_Index >= colors.length)
Color_Index = 0;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}