From 1e083f6d1efc510dda19dda1b59f4a2257073616 Mon Sep 17 00:00:00 2001
From: lizhanwei <Dana_Lee1016@126.com>
Date: 星期二, 14 一月 2020 15:16:59 +0800
Subject: [PATCH] 车辆实时位置绘图
---
app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java | 84 +++++++++++++++++++++++++++++++++++-------
1 files changed, 70 insertions(+), 14 deletions(-)
diff --git a/app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java b/app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java
index 986f967..f71e22f 100644
--- a/app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java
+++ b/app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java
@@ -1,5 +1,6 @@
package safeluck.drive.evaluation.fragment;
+import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -14,14 +15,28 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
+import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
+import com.anyun.exam.lib.MyLog;
+import com.google.gson.Gson;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.util.List;
+
import me.yokeyword.fragmentation.SupportFragment;
+import safeluck.drive.evaluation.Constant;
import safeluck.drive.evaluation.R;
+import safeluck.drive.evaluation.bean.RealTimeCarPos;
+import safeluck.drive.evaluation.cEventCenter.CEventCenter;
+import safeluck.drive.evaluation.cEventCenter.ICEventListener;
+import safeluck.drive.evaluation.util.CThreadPoolExecutor;
/**
@@ -30,18 +45,19 @@
* 閭锛�632393724@qq.com
* All Rights Saved! Chongqing AnYun Tech co. LTD
*/
-public class MapFragment extends SupportFragment{
+public class MapFragment extends SupportFragment {
private boolean isDrawing = false;
- public static SupportFragment newInstance(){
+ public static SupportFragment newInstance() {
return new MapFragment();
}
+
private SurfaceView mSurfaceView;
private static final String TAG = "MapFragment";
private SurfaceHolder holder;
Path path = new Path();
private Canvas canvas;
- private int x=-1;
+ private int x = -1;
private int y = -1;
private Toolbar toolbar;
@@ -52,15 +68,18 @@
private String osd = null;
private String osd2 = null;
private String osd3 = null;
-
-
+ double map[][] = {{1, 1}, {10, 1}, {10, 10}, {1, 10}};
+ double car[][] = {{8.278, 1.467}, {7.2780000000000009, 1.467}, {7.2780000000000009, -1.533}, {8.278, -1.533}
+ , {9.278, -1.5330000000000004}, {9.277999999999999, 1.467000000000001}};
+ Gson gson;
private LinearLayout linearLayout;//surfaceview鐨勭埗瀹瑰櫒锛屼负浜嗙Щ鍔╯urfaceview鑰屼娇鐢紝鍥犱负scrollto涓嶈兘绉诲姩view锛�
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.layout_mapfragment,container,false);
+ View view = inflater.inflate(R.layout.layout_mapfragment, container, false);
+ gson = new Gson();
initView(view);
return view;
}
@@ -108,7 +127,7 @@
holder.unlockCanvasAndPost(canvas);
}
- new Thread(new NeedForSpeed()).start();
+// new Thread(new NeedForSpeed(car, map)).start();
}
@Override
@@ -125,16 +144,21 @@
class NeedForSpeed implements Runnable {
+ public NeedForSpeed(double[][] car, double[][] map) {
+ DrawMap(map, car);
+ }
+
@Override
public void run() {
- double map[][] = {{1,1}, {10,1}, {10,10}, {1,10}};
- double car[][] = {{3,3}, {8,3}, {8,8}, {3,8}};
+
+// double car[][] = {{3,3}, {8,3}, {8,8}, {3,8}};
+
DrawMap(map, car);
}
}
- public void DrawMap(final double [][]map, final double [][]car) {
+ public void DrawMap(final double[][] map, final double[][] car) {
if (canvas2 == null || bmp == null) {
return;
}
@@ -205,10 +229,10 @@
Path path = new Path();
if (map.length != 9) {
- Log.d(TAG, "DrawMap X = " + String.format("%f", (float) (base_x + (map[0][0] - min_x) * scale_x)) + " Y = " + String.format("%f", (float) (base_y + (map[0][1] - min_y) * scale_y)));
+ Log.d(TAG, "DrawMap X = " + String.format("%f", (float) (base_x + (map[0][0] - min_x) * scale_x)) + " Y = " + String.format("%f", (float) (base_y + (map[0][1] - min_y) * scale_y)));
path.moveTo((float) (base_x + (map[0][0] - min_x) * scale_x), (float) (base_y + (map[0][1] - min_y) * scale_y));
for (int i = 1; i < map.length; i++) {
- Log.d(TAG, "DrawMap to X = " + (float) (base_x + (map[i][0] - min_x) * scale_x) + " Y = "+ (float) (base_y + (map[i][1] - min_y) * scale_y));
+ Log.d(TAG, "DrawMap to X = " + (float) (base_x + (map[i][0] - min_x) * scale_x) + " Y = " + (float) (base_y + (map[i][1] - min_y) * scale_y));
path.lineTo((float) (base_x + (map[i][0] - min_x) * scale_x), (float) (base_y + (map[i][1] - min_y) * scale_y));
}
path.close();
@@ -230,9 +254,9 @@
}
canvas2.drawPath(path, paint);
- path.moveTo((float) (base_x + (car[0][0] - min_x) * scale_x), (float) (base_y + (car[0][1] - min_y)*scale_y));
+ path.moveTo((float) (base_x + (car[0][0] - min_x) * scale_x), (float) (base_y + (car[0][1] - min_y) * scale_y));
for (int i = 1; i < car.length; i++)
- path.lineTo((float) (base_x + (car[i][0] - min_x) * scale_x), (float) (base_y + (car[i][1] - min_y)*scale_y));
+ path.lineTo((float) (base_x + (car[i][0] - min_x) * scale_x), (float) (base_y + (car[i][1] - min_y) * scale_y));
path.close();
canvas2.drawPath(path, paint);
@@ -289,7 +313,39 @@
});
}
+ int line = 0;
+ private ICEventListener icEventListener = new ICEventListener() {
+ @Override
+ public void onCEvent(String topic, int msgCode, int resultCode, Object obj) {
+ MyLog.i(TAG, (String) obj);
+ RealTimeCarPos timeCarPos = gson.fromJson((String) obj, RealTimeCarPos.class);
+ List<Double> points = timeCarPos.getPoint();
+ line = 0;
+ for (int i = 0; i < points.size(); i++) {
+ if ((i % 2) == 0) {
+ car[line][0] = points.get(i);
+ } else {
+ double value = 0 - points.get(i);
+ Log.i(TAG, "onCEvent: 鏂板��=" + value + " 浣嶇疆锛�" + i);
+ car[line][1] = value;
+ line++;
+ }
+ }
+ CThreadPoolExecutor.runInBackground(new NeedForSpeed(car,map));
+ }
+ };
+ @Override
+ public void onAttach(Context context) {
+ super.onAttach(context);
+ CEventCenter.onBindEvent(true, icEventListener, Constant.REAL_TIME_POS_CAR_TOPIC);
+ }
+
+ @Override
+ public void onDetach() {
+ super.onDetach();
+ CEventCenter.onBindEvent(false, icEventListener, Constant.REAL_TIME_POS_CAR_TOPIC);
+ }
}
--
Gitblit v1.8.0