yy1717
2020-05-15 da307bd35d279140d48d1bca3681deb975a723cf
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转化成Bitmap
     * @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, "解析地图1 " + buffer.toString().trim());
                        roadExamMap2 = gson.fromJson(buffer.toString().trim(), type2);
                    }else{
                        Log.d(TAG, "解析地图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);
                    }
                }