From aff3ecab64dd259fdbbf8382dc02325b46d7b4d1 Mon Sep 17 00:00:00 2001
From: lizhanwei <Dana_Lee1016@126.com>
Date: 星期二, 21 一月 2020 09:57:13 +0800
Subject: [PATCH] 修改地图为单线程绘制,收到的数据放入队列
---
app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java | 236 ++++++++++++++++++++++++++++++++++------------------------
1 files changed, 139 insertions(+), 97 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 8f30081..a711d55 100644
--- a/app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java
+++ b/app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java
@@ -6,8 +6,8 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
-import android.opengl.GLSurfaceView;
import android.os.Bundle;
+import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
@@ -15,7 +15,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
-import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -27,11 +26,11 @@
import com.google.gson.reflect.TypeToken;
-import org.json.JSONException;
-import org.json.JSONObject;
-
import java.lang.reflect.Type;
import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
import me.yokeyword.fragmentation.SupportFragment;
import safeluck.drive.evaluation.Constant;
@@ -40,7 +39,6 @@
import safeluck.drive.evaluation.bean.RealTimeCarPos;
import safeluck.drive.evaluation.cEventCenter.CEventCenter;
import safeluck.drive.evaluation.cEventCenter.ICEventListener;
-import safeluck.drive.evaluation.util.CThreadPoolExecutor;
import safeluck.drive.evaluation.util.FileUtil;
@@ -80,6 +78,9 @@
, {9.278, -1.5330000000000004}, {9.277999999999999, 1.467000000000001}};
Gson gson;
private LinearLayout linearLayout;//surfaceview鐨勭埗瀹瑰櫒锛屼负浜嗙Щ鍔╯urfaceview鑰屼娇鐢紝鍥犱负scrollto涓嶈兘绉诲姩view锛�
+ private LinkedBlockingQueue queue = new LinkedBlockingQueue(100);
+ private ExecutorService producer = Executors.newSingleThreadExecutor();
+ private ExecutorService consumer = Executors.newSingleThreadExecutor();
@Nullable
@@ -88,6 +89,7 @@
View view = inflater.inflate(R.layout.layout_mapfragment, container, false);
gson = new Gson();
initView(view);
+ consumer.execute(new CalRunnable());
return view;
}
@@ -282,12 +284,12 @@
pathText.lineTo(700, 30);
canvas2.drawTextOnPath(osd2, pathText, 0, 0, paint);//閫嗘椂閽堢敓鎴�
}
- if (osd3 != null) {
- Path pathText = new Path();
- pathText.moveTo(10, 240);
- pathText.lineTo(700, 130);
- canvas2.drawTextOnPath(osd2, pathText, 0, 0, paint);//閫嗘椂閽堢敓鎴�
- }
+// if (osd3 != null) {
+// Path pathText = new Path();
+// pathText.moveTo(10, 240);
+// pathText.lineTo(700, 130);
+// canvas2.drawTextOnPath(osd2, pathText, 0, 0, paint);//閫嗘椂閽堢敓鎴�
+// }
// 鎻愪氦鐢诲竷
Canvas canvas = holder.lockCanvas();
@@ -329,83 +331,116 @@
int line = 0;
int map_line = 0;
private StringBuffer stringBuffer = new StringBuffer();
+ class MessageRemoteService{
+ public int msgCode;
+ public String json;
+
+ public MessageRemoteService(int msgCode, Object obj) {
+ this.json = (String) obj;
+ this.msgCode = msgCode;
+ }
+ }
private ICEventListener icEventListener = new ICEventListener() {
@Override
- public void onCEvent(String topic, int msgCode, int resultCode, Object obj) {
+ public void onCEvent(String topic, final int msgCode, int resultCode, final Object obj) {
- CThreadPoolExecutor.runInBackground(new CalRunnable(msgCode,(String)obj));
+// CThreadPoolExecutor.runInBackground(new CalRunnable(msgCode,(String)obj));
+
+ producer.execute(new Runnable() {
+ @Override
+ public void run() {
+ queue.offer(new MessageRemoteService(msgCode,obj));
+ }
+ });
+
}
};
+ boolean flag = true;
class CalRunnable implements Runnable{
private String json;
private int cmd;
- public CalRunnable(int cmd,String obj) {
- this.json = obj;
- this.cmd = cmd;
- }
+// public CalRunnable(int cmd,String obj) {
+// this.json = obj;
+// this.cmd = cmd;
+// }
@Override
public void run() {
- MyLog.i(TAG, json);
- if (!(cmd == Constant.REAL_TIME_CAR_POS)){
- if (cmd == Constant.DEBUG_RTCM){
- stringBuffer.append("\tRTCM:");
+ while (flag){
+ MessageRemoteService messageRemoteService = (MessageRemoteService) queue.peek();
+ if (messageRemoteService == null){
try {
- JSONObject jsonObject = new JSONObject(json);
- int length = jsonObject.getInt("length");
- stringBuffer.append(length);
- } catch (JSONException e) {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
e.printStackTrace();
}
}
- if (cmd == Constant.DEBUG_TXT){
- try {
- JSONObject jsonObject = new JSONObject(json);
- String info = jsonObject.getString("info");
- stringBuffer.append("\tinfo:");
- stringBuffer.append(info);
- } catch (JSONException e) {
- e.printStackTrace();
+ messageRemoteService = (MessageRemoteService) queue.poll();
+ if (messageRemoteService != null){
+ this.cmd = messageRemoteService.msgCode;
+ this.json = messageRemoteService.json;
+ MyLog.i(TAG, json);
+// if (!(cmd == Constant.REAL_TIME_CAR_POS)){
+// if (cmd == Constant.DEBUG_RTCM){
+// stringBuffer.append("\tRTCM:");
+// try {
+// JSONObject jsonObject = new JSONObject(json);
+// int length = jsonObject.getInt("length");
+// stringBuffer.append(length);
+// } catch (JSONException e) {
+// e.printStackTrace();
+// }
+// }
+// if (cmd == Constant.DEBUG_TXT){
+// try {
+// JSONObject jsonObject = new JSONObject(json);
+// String info = jsonObject.getString("info");
+// stringBuffer.append("\tinfo:");
+// stringBuffer.append(info);
+// } catch (JSONException e) {
+// e.printStackTrace();
+// }
+//
+// }
+// }
+// osd3 = stringBuffer.toString();
+// if (osd3!=null && osd3.contains("info:")&&osd3.contains("RTCM:")){
+//
+// stringBuffer.delete(0,stringBuffer.length());
+// }
+
+
+ RealTimeCarPos timeCarPos = gson.fromJson((String) json, RealTimeCarPos.class);
+ List<Double> points = timeCarPos.getPoint();
+ switch (timeCarPos.getMove()){
+ case 0:
+ osd2 = "鍋滆溅";
+ break;
+ case 1:
+ osd2 = "鍓嶈繘";
+ break;
+ case -1:
+ osd2 = "鍚庨��";
+ break;
}
+ osd="鏂瑰悜瑙�"+String.valueOf(timeCarPos.getHeading());
+ line = 0;
+ map_line = 0;
- }
- }
- osd3 = stringBuffer.toString();
- if (osd3!=null && osd3.contains("info:")&&osd3.contains("RTCM:")){
+ 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++;
+ }
- stringBuffer.delete(0,stringBuffer.length());
- }
- RealTimeCarPos timeCarPos = gson.fromJson((String) json, RealTimeCarPos.class);
- List<Double> points = timeCarPos.getPoint();
- switch (timeCarPos.getMove()){
- case 0:
- osd2 = "鍋滆溅";
- break;
- case 1:
- osd2 = "鍓嶈繘";
- break;
- case -1:
- osd2 = "鍚庨��";
- break;
- }
- osd="鏂瑰悜瑙�"+String.valueOf(timeCarPos.getHeading());
- line = 0;
- map_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++;
- }
-
- }
- map_id = timeCarPos.getMap_id();
+ }
+ map_id = timeCarPos.getMap_id();
// 1 - 鍊掑簱
// 2 - 鍧¤捣
// 3 - 渚ф柟鍋滆溅
@@ -413,45 +448,49 @@
// 5 - 鐩磋杞集
- StringBuffer buffer = FileUtil.readTxtFileFromSD(_mActivity,Constant.MAP);
- Type type = new TypeToken<List<ExamMap>>(){}.getType();
- if (buffer != null){
- List<ExamMap> examMaps = gson.fromJson(buffer.toString().trim(), type);
+ StringBuffer buffer = FileUtil.readTxtFileFromSD(_mActivity,Constant.MAP);
+ Type type = new TypeToken<List<ExamMap>>(){}.getType();
+ if (buffer != null){
+ List<ExamMap> examMaps = gson.fromJson(buffer.toString().trim(), type);
- for (int i = 0; i < examMaps.size(); i++) {
- ExamMap examMap = examMaps.get(i);
- if (map_id == examMap.getId()){
- List<ExamMap.PointBean> pointBeanList = examMap.getPoint();
+ for (int i = 0; i < examMaps.size(); i++) {
+ ExamMap examMap = examMaps.get(i);
+ if (map_id == examMap.getId()){
+ List<ExamMap.PointBean> pointBeanList = examMap.getPoint();
- for(int j=0; j<pointBeanList.size();j++){
- List<Double> xys = pointBeanList.get(i).getXy();
- if(j==0){
- map = new double[xys.size()/2][2];
- }else{
- MyLog.i(TAG,"鏆傛椂杩樹笉鏀寔鏇茬嚎");
+ for(int j=0; j<pointBeanList.size();j++){
+ List<Double> xys = pointBeanList.get(i).getXy();
+ if(j==0){
+ map = new double[xys.size()/2][2];
+ }else{
+ MyLog.i(TAG,"鏆傛椂杩樹笉鏀寔鏇茬嚎");
+ break;
+ }
+
+ for (int k=0; k<xys.size();k++){
+ if ((k % 2) == 0) {
+ map[map_line][0] = xys.get(k);
+ } else {
+ double value = 0 - xys.get(k);
+ Log.i(TAG, "onCEvent: map鏂板��=" + value + " 浣嶇疆锛�" + i);
+ map[map_line][1] = value;
+ map_line++;
+ }
+ }
+
+ }
break;
}
-
- for (int k=0; k<xys.size();k++){
- if ((k % 2) == 0) {
- map[map_line][0] = xys.get(k);
- } else {
- double value = 0 - xys.get(k);
- Log.i(TAG, "onCEvent: map鏂板��=" + value + " 浣嶇疆锛�" + i);
- map[map_line][1] = value;
- map_line++;
- }
- }
-
}
- break;
}
+ DrawMap(map,car);
}
+
}
- DrawMap(map,car);
+
}
}
@Override
@@ -463,6 +502,9 @@
@Override
public void onDetach() {
super.onDetach();
+ flag = false;
+ producer.shutdown();
+ consumer.shutdown();
CEventCenter.onBindEvent(false, icEventListener, Constant.REAL_TIME_POS_CAR_TOPIC);
}
}
--
Gitblit v1.8.0