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/bean/RealTimeCarPos.java | 135 +++++++++++++++++++++++++++++++++ app/src/main/java/safeluck/drive/evaluation/fragment/MapFragment.java | 84 +++++++++++++++++--- 2 files changed, 205 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/RealTimeCarPos.java b/app/src/main/java/safeluck/drive/evaluation/bean/RealTimeCarPos.java index 59f41ce..cf82f07 100644 --- a/app/src/main/java/safeluck/drive/evaluation/bean/RealTimeCarPos.java +++ b/app/src/main/java/safeluck/drive/evaluation/bean/RealTimeCarPos.java @@ -1,4 +1,139 @@ package safeluck.drive.evaluation.bean; +import java.util.List; + public class RealTimeCarPos { + /** + * utc : 20200114035518.40 + * qf : 2 + * map_id : -1 + * move : -1 + * speed : 0.21068459839297035 + * heading : 0.0 + * main_ant : [8.278,-0.533] + * axial : [0,3] + * left_front_tire : [1,0] + * right_front_tire : [5,0] + * left_rear_tire : [2,3] + * right_rear_tire : [4,3] + * point : [8.278,1.467,7.2780000000000005,1.467,7.2780000000000005,-1.533,8.278,-1.533,9.278,-1.5330000000000004,9.277999999999999,1.467000000000001] + */ + + private String utc; + private int qf; + private int map_id; + private int move; + private double speed; + private double heading; + private List<Double> main_ant; + private List<Integer> axial; + private List<Integer> left_front_tire; + private List<Integer> right_front_tire; + private List<Integer> left_rear_tire; + private List<Integer> right_rear_tire; + private List<Double> point; + + public String getUtc() { + return utc; + } + + public void setUtc(String utc) { + this.utc = utc; + } + + public int getQf() { + return qf; + } + + public void setQf(int qf) { + this.qf = qf; + } + + public int getMap_id() { + return map_id; + } + + public void setMap_id(int map_id) { + this.map_id = map_id; + } + + public int getMove() { + return move; + } + + public void setMove(int move) { + this.move = move; + } + + public double getSpeed() { + return speed; + } + + public void setSpeed(double speed) { + this.speed = speed; + } + + public double getHeading() { + return heading; + } + + public void setHeading(double heading) { + this.heading = heading; + } + + public List<Double> getMain_ant() { + return main_ant; + } + + public void setMain_ant(List<Double> main_ant) { + this.main_ant = main_ant; + } + + public List<Integer> getAxial() { + return axial; + } + + public void setAxial(List<Integer> axial) { + this.axial = axial; + } + + public List<Integer> getLeft_front_tire() { + return left_front_tire; + } + + public void setLeft_front_tire(List<Integer> left_front_tire) { + this.left_front_tire = left_front_tire; + } + + public List<Integer> getRight_front_tire() { + return right_front_tire; + } + + public void setRight_front_tire(List<Integer> right_front_tire) { + this.right_front_tire = right_front_tire; + } + + public List<Integer> getLeft_rear_tire() { + return left_rear_tire; + } + + public void setLeft_rear_tire(List<Integer> left_rear_tire) { + this.left_rear_tire = left_rear_tire; + } + + public List<Integer> getRight_rear_tire() { + return right_rear_tire; + } + + public void setRight_rear_tire(List<Integer> right_rear_tire) { + this.right_rear_tire = right_rear_tire; + } + + public List<Double> getPoint() { + return point; + } + + public void setPoint(List<Double> point) { + this.point = point; + } } 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