From da307bd35d279140d48d1bca3681deb975a723cf Mon Sep 17 00:00:00 2001 From: yy1717 <fctom1215@outlook.com> Date: 星期五, 15 五月 2020 15:54:19 +0800 Subject: [PATCH] 坐标 --- app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java | 393 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 386 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java b/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java index 43558a3..d8bfc46 100644 --- a/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java +++ b/app/src/main/java/safeluck/drive/evaluation/fragment/RoadDriveMapFragmentaa.java @@ -2,9 +2,11 @@ import android.content.Context; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.DashPathEffect; +import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.os.Bundle; @@ -30,6 +32,7 @@ import org.json.JSONObject; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Type; import java.math.BigDecimal; import java.util.ArrayList; @@ -46,6 +49,7 @@ import safeluck.drive.evaluation.bean.ExamPlatformData; import safeluck.drive.evaluation.bean.RealTimeCarPos; import safeluck.drive.evaluation.bean.RoadExamMap; +import safeluck.drive.evaluation.bean.RoadExamMap2; import safeluck.drive.evaluation.cEventCenter.CEventCenter; import safeluck.drive.evaluation.cEventCenter.ICEventListener; import safeluck.drive.evaluation.util.CThreadPoolExecutor; @@ -87,7 +91,7 @@ } private SurfaceView mSurfaceView; - private static final String TAG = "RoadDriveFragment"; + private static final String TAG = "RoadDriveFragmentaa"; private SurfaceHolder holder; Path path = new Path(); private Canvas canvas; @@ -236,6 +240,8 @@ return newPoint; } + static int angg = 0; + public void DrawMap(double yaw, List<Double> mainAnt, double[][] map, double[][] car, List<Integer>body, List<Integer> tire, List<RoadExamMap.MapsBean> maps) { if (canvas2 == null || bmp == null) { @@ -255,7 +261,7 @@ for (int x = 0; x < map.length; x++) { PointF oldPoint = new PointF(map[x][0], map[x][1]); - PointF newPoint = rotatePoint(oldPoint, mainPoint, yaw ); + PointF newPoint = oldPoint;//rotatePoint(oldPoint, mainPoint, yaw ); map[x][0] = newPoint.getX(); map[x][1] = newPoint.getY(); @@ -266,7 +272,7 @@ for (int i = 0; i < car.length; i++) { PointF oldPoint = new PointF(car[i][0], car[i][1]); - PointF newPoint = rotatePoint(oldPoint, mainPoint, yaw ); + PointF newPoint = oldPoint;//rotatePoint(oldPoint, mainPoint, yaw ); car[i][0] = newPoint.getX(); car[i][1] = newPoint.getY(); @@ -465,11 +471,50 @@ } + + /*Bitmap bmpp = readBitmapById(R.drawable.police_car); + + double min_body_x, max_body_x, min_body_y, max_body_y; + + max_body_x = min_body_x = car[body.get(0)][0]; + min_body_y = max_body_y = car[body.get(0)][1]; + for (int i = 1; i < body.size(); i++){ + if (max_body_x < car[body.get(i)][0]) { + max_body_x = car[body.get(i)][0]; + } + if (min_body_x > car[body.get(i)][0]) { + min_body_x = car[body.get(i)][0]; + } + if (max_body_y < car[body.get(i)][1]) { + max_body_y = car[body.get(i)][1]; + } + if (min_body_y > car[body.get(i)][1]) { + min_body_y = car[body.get(i)][1]; + } + } + + + bmpp = scaleImage(bmpp, (int)((max_body_x - min_body_x)*scale_x), (int)((max_body_y - min_body_y)*scale_y)); + + Log.d(TAG, "鍥剧墖灏哄 " + bmpp.getWidth() + " " + bmpp.getHeight()); + + canvas2.drawBitmap(bmpp, 0, 0, paint); + + bmpp = rotaingImageView(angg, bmpp); + + Log.d(TAG, String.valueOf(angg) + " 鏃嬭浆鍚庣殑 鍥剧墖灏哄 " + bmpp.getWidth() + " " + bmpp.getHeight()); + + angg += 10; + + if (angg == 360) angg = 0; + + canvas2.drawBitmap(bmpp, 300, 200, paint);*/ + // canvas2.drawPath(path, paint); paint.setColor(Color.BLACK); path.moveTo((float) (base_x + (car[body.get(0)][0] - min_x) * scale_x), (float) (base_y + (car[body.get(0)][1] - min_y) * scale_y)); for (int i = 1; i < body.size(); i++){ - Log.d(TAG, "for 寰幆 DrawMap to X = " + (float) (base_x + (car[body.get(i)][0] - min_x) * scale_x)+ " Y = " + (float) (base_y + (car[body.get(i)][1] - min_y) * scale_y)); +// Log.d(TAG, "for 寰幆 DrawMap to X = " + (float) (base_x + (car[body.get(i)][0] - min_x) * scale_x)+ " Y = " + (float) (base_y + (car[body.get(i)][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (car[body.get(i)][0] - min_x) * scale_x), (float) (base_y + (car[body.get(i)][1] - min_y) * scale_y)); } @@ -662,6 +707,305 @@ } } + + public void DrawMap2(double yaw, List<Double> mainAnt, double[][] map, double[][] car, + List<Integer>body, List<Integer> tire, List<RoadExamMap2.RoadBean> roadBeanList, + List<RoadExamMap2.SpecialAreaBean> specialAreaBeanList) { + if (canvas2 == null || bmp == null) { + return; + } + float base_x = 300, base_y = 20; + double max_x = 0, min_x = 0, max_y = 0, min_y = 0; + long scale_x, scale_y; + + Log.d(TAG, "DrawMap2 map size " + map.length + " car size " + car.length); + + PointF mainPoint = new PointF(mainAnt.get(0), mainAnt.get(1)); + + Log.d(TAG, String.format("涓诲ぉ绾� X = %f Y = %f 鏃嬭浆瑙掑害 %f", mainPoint.getX(), mainPoint.getY(), yaw)); + + + for (int x = 0; x < map.length; x++) { + PointF oldPoint = new PointF(map[x][0], map[x][1]); + PointF newPoint = rotatePoint(oldPoint, mainPoint, yaw ); + map[x][0] = newPoint.getX(); + map[x][1] = newPoint.getY(); + + map[x][0] = map[x][0] - mainPoint.getX(); + map[x][1] = map[x][1] - mainPoint.getY(); + map[x][1] = -map[x][1]; + } + + for (int i = 0; i < car.length; i++) { + PointF oldPoint = new PointF(car[i][0], car[i][1]); + PointF newPoint = rotatePoint(oldPoint, mainPoint, yaw ); + car[i][0] = newPoint.getX(); + car[i][1] = newPoint.getY(); + + car[i][0] = car[i][0] - mainPoint.getX(); + car[i][1] = car[i][1] - mainPoint.getY(); + car[i][1] = -car[i][1]; + } + + scale_x = Math.round(Math.abs(50 / Math.sqrt(Math.pow(car[0][0], 2) + Math.pow(car[0][1], 2)) )); + scale_y = scale_x; + + base_x = bmp.getWidth() / 2; + base_y = bmp.getHeight() * 7 / 10; + + Log.d(TAG, String.format("DrawMapAll scale_x = %d 杞﹀ご鎹� = %f", scale_x, Math.sqrt(Math.pow(car[0][0], 2) + Math.pow(car[0][1], 2)))); + + canvas2.drawColor(Color.WHITE); + + // 鐢昏溅杞� + paint.setStyle(Paint.Style.FILL_AND_STROKE); + paint.setColor(Color.BLUE); + + canvas2.drawCircle((float) (base_x), (float) (base_y), 2, paint); + + paint.setColor(Color.RED); + canvas2.drawCircle((float) (base_x + (car[tire.get(0)][0] - min_x) * scale_x), (float) (base_y + (car[tire.get(0)][1] - min_y) * scale_y), 2.5f, paint); + canvas2.drawCircle((float) (base_x + (car[tire.get(1)][0] - min_x) * scale_x), (float) (base_y + (car[tire.get(1)][1] - min_y) * scale_y), 2.5f, paint); + canvas2.drawCircle((float) (base_x + (car[tire.get(2)][0] - min_x) * scale_x), (float) (base_y + (car[tire.get(2)][1] - min_y) * scale_y), 2.5f, paint); + canvas2.drawCircle((float) (base_x + (car[tire.get(3)][0] - min_x) * scale_x), (float) (base_y + (car[tire.get(3)][1] - min_y) * scale_y), 2.5f, paint); + + paint.setStyle(Paint.Style.STROKE); + paint.setColor(Color.BLACK); + + Path path = new Path(); + // 鐢昏溅浣� + path.moveTo((float) (base_x + (car[body.get(0)][0] - min_x) * scale_x), (float) (base_y + (car[body.get(0)][1] - min_y) * scale_y)); + for (int i = 1; i < body.size(); i++){ +// Log.d(TAG, "for 寰幆 DrawMap to X = " + (float) (base_x + (car[body.get(i)][0] - min_x) * scale_x)+ " Y = " + (float) (base_y + (car[body.get(i)][1] - min_y) * scale_y)); + path.lineTo((float) (base_x + (car[body.get(i)][0] - min_x) * scale_x), (float) (base_y + (car[body.get(i)][1] - min_y) * scale_y)); + } + + path.close(); + + canvas2.drawPath(path, paint); + + // 鐢诲湴鍥� + if (roadBeanList != null) { + for (int x = 0; x < roadBeanList.size(); x++) { + RoadExamMap2.RoadBean road = roadBeanList.get(x); + Log.d(TAG, "璺� id = "+ road.getId()); + + List<RoadExamMap2.RoadBean.EdgeBean> leftEdge = road.getLeft_edge(); + + if (leftEdge != null) { + Log.d(TAG, "缁樺埗宸﹁矾娌�"); + for (int y = 0; y < leftEdge.size(); y++) { + RoadExamMap2.RoadBean.EdgeBean edgeBean = leftEdge.get(y); + + if (edgeBean.getType() == 0) { + paint.setPathEffect(new DashPathEffect(new float[] {20, 20}, 0)); + } else { + paint.setPathEffect(new DashPathEffect(new float[] {10000, 0}, 0)); + } + + List<Integer> line = edgeBean.getLine(); + Log.d(TAG, "璺竟娌� " + y + " 鍒嗘 " + line.size()); + path.moveTo((float) (base_x + (map[line.get(0)][0] - min_x) * scale_x), (float) (base_y + (map[line.get(0)][1] - min_y) * scale_y)); + + Log.d(TAG, String.format("0. X = %f Y = %f", map[line.get(0)][0], map[line.get(0)][1])); + + for (int z = 1; z < line.size(); z++) { + path.lineTo((float) (base_x + (map[line.get(z)][0] - min_x) * scale_x), (float) (base_y + (map[line.get(z)][1] - min_y) * scale_y)); + Log.d(TAG, String.format("%d. X = %f Y = %f", z, map[line.get(z)][0], map[line.get(z)][1])); + } + Log.d(TAG, "璺竟娌�......"); + canvas2.drawPath(path, paint); + Log.d(TAG, "璺竟娌垮畬姣�"); + } + } + + List<RoadExamMap2.RoadBean.EdgeBean> rightEdge = road.getRight_edge(); + + if (rightEdge != null) { + Log.d(TAG, "缁樺埗鍙宠矾娌�"); + for (int y = 0; y < rightEdge.size(); y++) { + RoadExamMap2.RoadBean.EdgeBean edgeBean = rightEdge.get(y); + + if (edgeBean.getType() == 0) { + Log.d(TAG, "铏氱嚎"); + paint.setPathEffect(new DashPathEffect(new float[] {20, 20}, 0)); + } else { + Log.d(TAG, "瀹炵嚎"); + paint.setPathEffect(new DashPathEffect(new float[] {10000, 0}, 0)); + } + + List<Integer> line = edgeBean.getLine(); + + Path edgePath = new Path(); + Log.d(TAG, "璺竟娌� " + y + " 鍒嗘 " + line.size()); + edgePath.moveTo((float) (base_x + (map[line.get(0)][0] - min_x) * scale_x), (float) (base_y + (map[line.get(0)][1] - min_y) * scale_y)); + + Log.d(TAG, String.format("0. X = %f Y = %f", map[line.get(0)][0], map[line.get(0)][1])); + + for (int z = 1; z < line.size(); z++) { + edgePath.lineTo((float) (base_x + (map[line.get(z)][0] - min_x) * scale_x), (float) (base_y + (map[line.get(z)][1] - min_y) * scale_y)); + Log.d(TAG, String.format("%d. X = %f Y = %f", z, map[line.get(z)][0], map[line.get(z)][1])); + } + Log.d(TAG, "璺竟娌�......"); + canvas2.drawPath(edgePath, paint); + Log.d(TAG, "璺竟娌垮畬姣�"); + } + } + + List<List<List<RoadExamMap2.RoadBean.SeparateBean>>> separate = road.getSeparate(); + + if (separate != null) { + Log.d(TAG, "缁樺埗閬撹矾鍒嗗壊绾� 娈垫暟閲� = " + separate.size()); + + + for (int y = 0; y < separate.size(); y++) { + List<List<RoadExamMap2.RoadBean.SeparateBean>> lines = separate.get(y); + + Log.d(TAG, "绾挎暟閲� = " + lines.size()); + + for (int z = 0; z < lines.size(); z++) { + List<RoadExamMap2.RoadBean.SeparateBean> line = lines.get(z); + + for (int zz = 0; zz < line.size(); zz++) { + RoadExamMap2.RoadBean.SeparateBean seg = line.get(zz); + + Path sepPath = new Path(); + + if (seg.getType() == 0) { + Log.d(TAG, "铏氱嚎"); + paint.setPathEffect(new DashPathEffect(new float[] {40, 80}, 0)); + } else { + Log.d(TAG, "瀹炵嚎"); + paint.setPathEffect(new DashPathEffect(new float[] {10000, 0}, 0)); + } + + List<Integer> points = seg.getLine(); + + + sepPath.moveTo((float) (base_x + (map[points.get(0)][0] - min_x) * scale_x), (float) (base_y + (map[points.get(0)][1] - min_y) * scale_y)); + for (int za = 1; za < points.size(); za++) { + sepPath.lineTo((float) (base_x + (map[points.get(za)][0] - min_x) * scale_x), (float) (base_y + (map[points.get(za)][1] - min_y) * scale_y)); + } + canvas2.drawPath(sepPath, paint); + } + } + + + /*if (line.getType() == 0) { + Log.d(TAG, "铏氱嚎"); + paint.setPathEffect(new DashPathEffect(new float[] {40, 80}, 0)); + } else { + Log.d(TAG, "瀹炵嚎"); + paint.setPathEffect(new DashPathEffect(new float[] {10000, 0}, 0)); + } + + List<List<Integer>> lines = sep.getLine(); + + Path sepPath = new Path(); + + for (int z = 0; z < lines.size(); z++) { + + List<Integer> line = lines.get(z); + + sepPath.moveTo((float) (base_x + (map[line.get(0)][0] - min_x) * scale_x), (float) (base_y + (map[line.get(0)][1] - min_y) * scale_y)); + + for (int za = 1; za < line.size(); za++) { + sepPath.lineTo((float) (base_x + (map[line.get(za)][0] - min_x) * scale_x), (float) (base_y + (map[line.get(za)][1] - min_y) * scale_y)); + } + canvas2.drawPath(sepPath, paint); + + }*/ + } + } + + + } + } + + if (specialAreaBeanList != null) { + for (x = 0; x < specialAreaBeanList.size(); x++) { + RoadExamMap2.SpecialAreaBean specialArea = specialAreaBeanList.get(x); + + List<Integer> points = specialArea.getArea(); + + for (int y = 0; y < points.size(); y++) { + canvas2.drawCircle((float) (base_x + (map[points.get(y)][0] - min_x) * scale_x), (float) (base_y + (map[points.get(y)][1] - min_x) * scale_y), 2.5f, paint); + } + } + } + + + // 鎻愪氦鐢诲竷 + Canvas canvas = holder.lockCanvas(); + if (canvas != null){ + + canvas.drawBitmap(bmp, 0, 0, paint); +// canvas.drawBitmap(bmp2, screen_width*2/3, 0, paint); + holder.unlockCanvasAndPost(canvas); + Log.d(TAG, "鎻愪氦鐢诲竷"); + } + } + /** + * 閫氳繃璧勬簮id杞寲鎴怋itmap + * @param context + * @param resId + * @return + */ + public Bitmap readBitmapById(int resId){ +// BitmapFactory.Options opt = new BitmapFactory.Options(); +// opt.inPreferredConfig = Bitmap.Config.ARGB_8888; + InputStream is = getResources().openRawResource(resId); + return BitmapFactory.decodeStream(is); + } + + + /** + * 缂╂斁鍥剧墖 + * @param bm 瑕佺缉鏀惧浘鐗� + * @param newWidth 瀹藉害 + * @param newHeight 楂樺害 + * @return澶勭悊鍚庣殑鍥剧墖 + */ + public Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight){ + if (bm == null){ + return null; + } + int width = bm.getWidth(); + int height = bm.getHeight(); + float scaleWidth = ((float) newWidth) / width; + float scaleHeight = ((float) newHeight) / height; + Matrix matrix = new Matrix(); + matrix.postScale(scaleWidth, scaleHeight); + Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,true); + if (bm != null & !bm.isRecycled()){ + bm.recycle();//閿�姣佸師鍥剧墖 + bm = null; + } + return newbm; + } + + /** + * 鏃嬭浆鍥剧墖 + * @param angle 鏃嬭浆瑙掑害 + * @param bitmap 瑕佸鐞嗙殑Bitmap + * @return 澶勭悊鍚庣殑Bitmap + */ + public Bitmap rotaingImageView(int angle, Bitmap bitmap) + { + // 鏃嬭浆鍥剧墖 鍔ㄤ綔 + Matrix matrix = new Matrix(); + matrix.postRotate(angle); + // 鍒涘缓鏂扮殑鍥剧墖 + Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, + bitmap.getWidth(), bitmap.getHeight(), matrix, true); + if (resizedBitmap != bitmap && bitmap != null && !bitmap.isRecycled()){ + bitmap.recycle(); + bitmap = null; + } + return resizedBitmap; + } + + int line = 0; int map_line = 0; class MessageRemoteService{ @@ -709,12 +1053,14 @@ e.printStackTrace(); } } + messageRemoteService = (MessageRemoteService) queue.poll(); if (messageRemoteService != null) { - MyLog.i(TAG, messageRemoteService.json); + MyLog.i(TAG, messageRemoteService.json+" 娑堟伅闃熷垪闀垮害锛�"+queue.size()); RealTimeCarPos timeCarPos = gson.fromJson((String) messageRemoteService.json, RealTimeCarPos.class); + List<Double> points = timeCarPos.getPoint(); switch (timeCarPos.getMove()) { case 0: @@ -785,7 +1131,7 @@ } - Type type = new TypeToken<RoadExamMap>() { + /*Type type = new TypeToken<RoadExamMap>() { }.getType(); if (buffer != null) { examMaps = gson.fromJson(buffer.toString().trim(), type); @@ -810,7 +1156,40 @@ } } - DrawMap(yaw, mainAnt,map, car, body, tire, examMaps.getMaps()); + DrawMap(yaw, mainAnt,map, car, body, tire, examMaps.getMaps());*/ + + + RoadExamMap2 roadExamMap2 = null; + Type type2 = new TypeToken<RoadExamMap2>() { + }.getType(); + + if (buffer != null) { + Log.d(TAG, "瑙f瀽鍦板浘1 " + buffer.toString().trim()); + roadExamMap2 = gson.fromJson(buffer.toString().trim(), type2); + }else{ + Log.d(TAG, "瑙f瀽鍦板浘2 " + newmap.trim()); + roadExamMap2 = gson.fromJson(newmap.trim(),type2); + } + if (roadExamMap2 != null) { + points = roadExamMap2.getPoints(); + Log.d(TAG, "鍦板浘鏁伴噺 %d" + points.size()); + if (points != null) { + map = new double[points.size() / 2][2]; + for (int i = 0; i < points.size(); i++) { + if ((i % 2) == 0) { + map[map_line][0] = points.get(i); + + } else { + double value = points.get(i); + map[map_line][1] = value; + map_line++; + } + } + } + + DrawMap2(yaw, mainAnt, map, car, body, tire, roadExamMap2.getRoadBean(), roadExamMap2.getSpecialAreaBean()); +// DrawMap(yaw, mainAnt, map, car, body, tire, null); + } } -- Gitblit v1.8.0