package safeluck.drive.evaluation.fragment; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.os.Bundle; import android.os.Handler; import android.text.LoginFilter; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.SurfaceHolder; import android.view.SurfaceView; 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.AYSdk; import com.anyun.exam.lib.MyLog; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; 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; import safeluck.drive.evaluation.R; import safeluck.drive.evaluation.bean.ExamMap; import safeluck.drive.evaluation.bean.ExamPlatformData; import safeluck.drive.evaluation.bean.Point; 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; /** * MyApplication2 * Created by lzw on 2019/9/26. 15:20:58 * 邮箱:632393724@qq.com * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class MapFragment extends SupportFragment implements View.OnClickListener { private boolean isDrawing = false; private int pixels = 80; private ICEventListener rtcmicEventListener = new ICEventListener() { @Override public void onCEvent(String topic, int msgCode, int resultCode, final Object obj) { if (msgCode == Constant.DEBUG_RTCM){ CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Log.i(TAG, "title="+(String)obj); toolbar.setTitle((String)obj); } }); } } }; private ICEventListener debugTxticEventListener = new ICEventListener() { @Override public void onCEvent(String topic, int msgCode, int resultCode, final Object obj) { if (msgCode == Constant.DEBUG_TXT){ CThreadPoolExecutor.runOnMainThread(new Runnable() { @Override public void run() { Log.i(TAG, "subtitle="+(String)obj); toolbar.setSubtitle((String)obj); } }); } } }; private ICEventListener speedListener = new ICEventListener() { @Override public void onCEvent(String topic, int msgCode, int resultCode, Object obj) { if (msgCode == Constant.RTK_INFO){ gpsSpeed = (double)obj; } } }; 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 y = -1; private Toolbar toolbar; private Bitmap bmp = null; private Bitmap bmp2 = null; private Canvas canvas2 = null; private Canvas canvas3 = null; private Paint paint = null; private int screen_width = 0, screen_height = 0; private String osdHeading = null; private String osdMoveDirect = null; private String osdRtkSpeed = null; private String osdQf = null; private int map_id; private double gpsSpeed = 0; double allMap[][][]; double map[][] = {{-2.4065, 30.7090}, {-3.0759, 30.3599}, { -2.6023, 29.2578}, {-3.3316,28.9383},{-3.8247,30.0293}, {-3.4503,30.1946}, {-2.9669,29.0981}, { -3.6962, 28.7786}, {-4.1991, 29.8640}}; double map_other[][] = {{-2.4065, 30.7090}, {-3.0759, 30.3599}, { -2.6023, 29.2578}, {-3.3316,28.9383},{-3.8247,30.0293}, {-3.4503,30.1946}, {-2.9669,29.0981}}; // double map[][] ; 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的父容器,为了移动surfaceview而使用,因为scrollto不能移动view, private LinkedBlockingQueue queue = new LinkedBlockingQueue(100); private ExecutorService producer = Executors.newSingleThreadExecutor(); private ExecutorService consumer = Executors.newSingleThreadExecutor(); @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout_mapfragment, container, false); gson = new Gson(); initView(view); consumer.execute(new CalRunnable()); return view; } private int avaliableHeight,avalibleWidth; private void initView(View view) { avaliableHeight= getResources().getDisplayMetrics().heightPixels; avalibleWidth = getResources().getDisplayMetrics().widthPixels; Log.i(TAG, "initView: aliwidht="+avalibleWidth+" height="+avaliableHeight); mSurfaceView = view.findViewById(R.id.surfaceview); view.findViewById(R.id.btn_reduce).setOnClickListener(this); view.findViewById(R.id.btn_amplify).setOnClickListener(this); holder = mSurfaceView.getHolder(); holder.addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { Canvas canvas = surfaceHolder.lockCanvas(); if (canvas != null) { Log.d(TAG, "W = " + canvas.getWidth() + " H = " + canvas.getHeight()); screen_width = canvas.getWidth(); screen_height = canvas.getHeight(); surfaceHolder.unlockCanvasAndPost(canvas); bmp = Bitmap.createBitmap(screen_width*2/3, screen_height, Bitmap.Config.ARGB_8888); canvas2 = new Canvas(bmp); canvas2.drawColor(Color.WHITE); bmp2 = Bitmap.createBitmap(screen_width/3, screen_height, Bitmap.Config.ARGB_8888); canvas3 = new Canvas(bmp2); canvas3.drawColor(Color.WHITE); Log.d(TAG, "BMP W = " + bmp.getWidth() + "BMP H = " + bmp.getHeight()); paint = new Paint(); paint.setTextSize(30); paint.setColor(Color.BLACK); paint.setStrokeWidth(1.5f); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); holder.lockCanvas(); canvas.drawBitmap(bmp2, screen_width/3, 0, paint); canvas.drawBitmap(bmp, 0, 0, paint); holder.unlockCanvasAndPost(canvas); } } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } }); } public void DrawMap(List mainAnt, final double[][] map, final double[][] car, Listbody, List tire) { if (canvas2 == null || bmp == null) { return; } // _mActivity.runOnUiThread(new Runnable() { // @Override // public void run() { double base_x = 300, base_y = 20; double max_x = 0, min_x = 0, max_y = 0, min_y = 0; Log.d(TAG, "DrawMap map size " + map.length + " car size " + car.length); for (int i = 0; i < map.length; i++) { if (i == 0) { max_x = map[0][0]; min_x = map[0][0]; max_y = map[0][1]; min_y = map[0][1]; } else { if (max_x < map[i][0]) { max_x = map[i][0]; } if (min_x > map[i][0]) { min_x = map[i][0]; } if (max_y < map[i][1]) { max_y = map[i][1]; } if (min_y > map[i][1]) { min_y = map[i][1]; } } } if (map_other.length>9){ for (int i = 0; i < map_other.length; i++) { if (max_x < map_other[i][0]) { max_x = map_other[i][0]; } if (min_x > map_other[i][0]) { min_x = map_other[i][0]; } if (max_y < map_other[i][1]) { max_y = map_other[i][1]; } if (min_y > map_other[i][1]) { min_y = map_other[i][1]; } } } Log.d(TAG, "DrawMap max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); for (int i = 0; i < car.length; i++) { if (max_x < car[i][0]) { max_x = car[i][0]; } if (min_x > car[i][0]) { min_x = car[i][0]; } if (max_y < car[i][1]) { max_y = car[i][1]; } if (min_y > car[i][1]) { min_y = car[i][1]; } } Log.d(TAG, "DrawMap max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); long scale_x = Math.round((bmp.getWidth() - base_x - 10) / (max_x - min_x)); long scale_y = Math.round((bmp.getHeight() - base_y - 10) / (max_y - min_y)); if (scale_x >= scale_y) { scale_x = scale_y; } else { scale_y = scale_x; } Log.d(TAG, "DrawMap scale_x " + scale_x + " scale_y " + scale_y); canvas2.drawColor(Color.WHITE); if (paint == null || canvas2==null){ return; } paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setColor(Color.BLUE); canvas2.drawCircle((float) (base_x + (mainAnt.get(0) - min_x) * scale_x), (float) (base_y + (0 - mainAnt.get(1) - min_y) * scale_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(); Log.i(TAG, "DrawMap: map.length:"+map.length); 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))); 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)); path.lineTo((float) (base_x + (map[i][0] - min_x) * scale_x), (float) (base_y + (map[i][1] - min_y) * scale_y)); } path.close(); } else if (map.length == 9) { path.moveTo((float) (base_x + (map[0][0] - min_x) * scale_x), (float) (base_y + (map[0][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[8][0] - min_x) * scale_x), (float) (base_y + (map[8][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[2][0] - min_x) * scale_x), (float) (base_y + (map[2][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[1][0] - min_x) * scale_x), (float) (base_y + (map[1][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[3][0] - min_x) * scale_x), (float) (base_y + (map[3][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[4][0] - min_x) * scale_x), (float) (base_y + (map[4][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[6][0] - min_x) * scale_x), (float) (base_y + (map[6][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[5][0] - min_x) * scale_x), (float) (base_y + (map[5][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[7][0] - min_x) * scale_x), (float) (base_y + (map[7][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[8][0] - min_x) * scale_x), (float) (base_y + (map[8][1] - min_y) * scale_y)); } else { 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++) { path.lineTo((float) (base_x + (map[i][0] - min_x) * scale_x), (float) (base_y + (map[i][1] - min_y) * scale_y)); } if (map_other.length>9){ path.moveTo((float) (base_x + (map_other[0][0] - min_x) * scale_x), (float) (base_y + (map_other[0][1] - min_y) * scale_y)); for (int i = 1; i < map.length; i++) { path.lineTo((float) (base_x + (map_other[i][0] - min_x) * scale_x), (float) (base_y + (map_other[i][1] - min_y) * scale_y)); } } for (int i = 0, j = 0; i < map.length && j < map_other.length; i++, j++) { path.moveTo((float) (base_x + (map[i][0] - min_x) * scale_x), (float) (base_y + (map[i][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map_other[j][0] - min_x) * scale_x), (float) (base_y + (map_other[j][1] - min_y) * scale_y)); } } canvas2.drawPath(path, paint); 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); paint.setStyle(Paint.Style.FILL_AND_STROKE); if (osdHeading != null) { Path pathText = new Path(); pathText.moveTo(10, 170); pathText.lineTo(700, 170); canvas2.drawTextOnPath(osdHeading, pathText, 0, 0, paint);//逆时针生成 } if (osdMoveDirect != null) { Path pathText = new Path(); pathText.moveTo(10, 30); pathText.lineTo(700, 30); canvas2.drawTextOnPath(osdMoveDirect, pathText, 0, 0, paint);//逆时针生成 } if (osdRtkSpeed != null) { Path pathText = new Path(); pathText.moveTo(10, 240); pathText.lineTo(700, 240); canvas2.drawTextOnPath(osdRtkSpeed, pathText, 0, 0, paint);//逆时针生成 } if (osdQf != null) { Path pathText = new Path(); pathText.moveTo(10, 120); pathText.lineTo(700, 120); canvas2.drawTextOnPath(osdQf, pathText, 0, 0, paint);//逆时针生成 } { BigDecimal bd = new BigDecimal(gpsSpeed); bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP); Path pathText = new Path(); pathText.moveTo(10, 70); pathText.lineTo(700, 70); canvas2.drawTextOnPath("GPS速度:" + bd, pathText, 0, 0, paint);//逆时针生成 } // 提交画布 Canvas canvas = holder.lockCanvas(); if (canvas != null){ canvas.drawBitmap(bmp, 0, 0, paint); holder.unlockCanvasAndPost(canvas); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_reduce: if (pixels < 5){ pixels = 5; Toast.makeText(_mActivity, "已经是最小了,不能再小了", Toast.LENGTH_SHORT).show(); }else{ pixels -= 5; } break; case R.id.btn_amplify: if (pixels >80){ Toast.makeText(_mActivity, "已经是最大了", Toast.LENGTH_SHORT).show(); pixels = 80; }else{ pixels+=5; } break; } } class PointF { double x; double y; public PointF(double x, double y) { this.x = x; this.y = y; } double getX() { return x; } double getY() { return y; } void setX(double x) { this.x = x; } void setY(double y) { this.y = y; } } public PointF rotatePoint(PointF oldPoint, PointF centre, double degree) { PointF newPoint = new PointF(0, 0); newPoint.setX( (oldPoint.getX()-centre.getX())*Math.cos(Math.toRadians(degree)) - (oldPoint.getY()-centre.getY())*Math.sin(Math.toRadians(degree)) + centre.getX() ); newPoint.setY ( (oldPoint.getX()-centre.getX())*Math.sin(Math.toRadians(degree)) + (oldPoint.getY()-centre.getY())*Math.cos(Math.toRadians(degree)) + centre.getY() ); return newPoint; } public void DrawMapAll(double yaw, List mainAnt, double[][][] map, double [][]map_other, double[][] car, Listbody, List tire) { if (canvas2 == null || bmp == null || bmp2 == null || canvas3 == null) { return; } long scale_x,scale_y; // _mActivity.runOnUiThread(new Runnable() { // @Override // public void run() { double base_x = 300, base_y = 20; double max_x = 0, min_x = 0, max_y = 0, min_y = 0; // Log.d(TAG, "DrawMap 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++) { for (int j = 0; j < map[x].length; j++) { PointF oldPoint = new PointF(map[x][j][0], map[x][j][1]); PointF newPoint = rotatePoint(oldPoint, mainPoint, yaw ); map[x][j][0] = newPoint.getX(); map[x][j][1] = newPoint.getY(); map[x][j][0] = map[x][j][0] - mainPoint.getX(); map[x][j][1] = map[x][j][1] - mainPoint.getY(); map[x][j][1] = -map[x][j][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]; } for (int i = 0; i < map_other.length; i++) { PointF oldPoint = new PointF(map_other[i][0], map_other[i][1]); PointF newPoint = rotatePoint(oldPoint, mainPoint, yaw ); map_other[i][0] = newPoint.getX(); map_other[i][1] = newPoint.getY(); map_other[i][0] = map_other[i][0] - mainPoint.getX(); map_other[i][1] = map_other[i][1] - mainPoint.getY(); map_other[i][1] = -map_other[i][1]; } scale_x = Math.round(Math.abs(pixels / 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)))); /* for (int x = 0; x < map.length; x++) { for (int j = 0; j < map[x].length; j++) { if (x == 0&&j==0) { max_x = map[0][0][0]; min_x = map[0][0][0]; max_y = map[0][0][1]; min_y = map[0][0][1]; } else { // Log.i(TAG,String.format("map[%d][%d][0]=%f",x,j,map[x][j][0])); // Log.i(TAG,String.format("map[%d][%d][1]=%f",x,j,map[x][j][1])); // Log.i(TAG,String.format("max_x=%f,max_y=%f,min_x=%f,min_y=%f",max_x,max_y,min_x,min_y)); if (max_x < map[x][j][0]) { max_x = map[x][j][0]; } if (min_x > map[x][j][0]) { min_x = map[x][j][0]; } if (max_y < map[x][j][1]) { max_y = map[x][j][1]; } if (min_y > map[x][j][1]) { min_y = map[x][j][1]; } } } } // Log.d(TAG, "all 地图DrawMap max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); if (map_other.length>9){ for (int i = 0; i < map_other.length; i++) { if (max_x < map_other[i][0]) { max_x = map_other[i][0]; } if (min_x > map_other[i][0]) { min_x = map_other[i][0]; } if (max_y < map_other[i][1]) { max_y = map_other[i][1]; } if (min_y > map_other[i][1]) { min_y = map_other[i][1]; } } } // Log.d(TAG, "DrawMap另外一条曲线 max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); for (int i = 0; i < car.length; i++) { if (max_x < car[i][0]) { max_x = car[i][0]; } if (min_x > car[i][0]) { min_x = car[i][0]; } if (max_y < car[i][1]) { max_y = car[i][1]; } if (min_y > car[i][1]) { min_y = car[i][1]; } } // Log.d(TAG, "DrawMapAll 和车比max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); scale_x = Math.round((screen_width - base_x - 10) / (max_x - min_x)); scale_y = Math.round((screen_height - base_y - 10) / (max_y - min_y)); if (scale_x >= scale_y) { scale_x = scale_y; } else { scale_y = scale_x; }*/ // Log.d(TAG, "DrawMapAll scale_x " + scale_x + " scale_y " + scale_y); canvas2.drawColor(Color.WHITE); if (paint == null || canvas2==null){ return; } paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setColor(Color.BLUE); // canvas2.drawCircle((float) (base_x + (mainAnt.get(0) - min_x) * scale_x), (float) (base_y + (mainAnt.get(1) - min_y) * scale_y), 2, paint); 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); canvas2.drawCircle((float) (base_x + (car[tire.get(0)][0]) * scale_x), (float) (base_y + (car[tire.get(0)][1]) * scale_y), 2.5f, paint); canvas2.drawCircle((float) (base_x + (car[tire.get(1)][0]) * scale_x), (float) (base_y + (car[tire.get(1)][1]) * scale_y), 2.5f, paint); canvas2.drawCircle((float) (base_x + (car[tire.get(2)][0]) * scale_x), (float) (base_y + (car[tire.get(2)][1]) * scale_y), 2.5f, paint); canvas2.drawCircle((float) (base_x + (car[tire.get(3)][0]) * scale_x), (float) (base_y + (car[tire.get(3)][1]) * scale_y), 2.5f, paint); paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); Path path = new Path(); for (int k = 0; k < map.length; k++) { Log.i(TAG, String.format("all DrawMap: map[%d].length=[%d]",k,map[k].length)); if (map[k].length < 9) { // Log.d(TAG, String.format("map[%d][0][0]=%f,X=%f",k,map[k][0][0], (float) (base_x + (map[k][0][0] - min_x) * scale_x)) + String.format("map[%d][0][1]=%f,Y=%f",k,map[k][0][1], (float) (base_y + (map[k][0][1] - min_y) * scale_y))); path.moveTo((float) (base_x + (map[k][0][0]) * scale_x), (float) (base_y + (map[k][0][1]) * scale_y)); for (int i = 1; i < map[k].length; i++) { // Log.i(TAG,String.format("map[%d][%d][0]=%f,to X=%f",k,i,map[k][i][0],(float) (base_x + (map[k][i][0] - min_x) * scale_x))); // Log.i(TAG,String.format("map[%d][%d][1]=%f,to Y=%f",k,i,map[k][i][1],(float) (base_y + (map[k][i][1] - min_y) * scale_y))); path.lineTo((float) (base_x + (map[k][i][0]) * scale_x), (float) (base_y + (map[k][i][1]) * scale_y)); } path.close(); } else if (map[k].length == 9) { path.moveTo((float) (base_x + (map[k][0][0] - min_x) * scale_x), (float) (base_y + (map[k][0][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][8][0] - min_x) * scale_x), (float) (base_y + (map[k][8][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][2][0] - min_x) * scale_x), (float) (base_y + (map[k][2][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][1][0] - min_x) * scale_x), (float) (base_y + (map[k][1][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][3][0] - min_x) * scale_x), (float) (base_y + (map[k][3][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][4][0] - min_x) * scale_x), (float) (base_y + (map[k][4][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][6][0] - min_x) * scale_x), (float) (base_y + (map[k][6][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][5][0] - min_x) * scale_x), (float) (base_y + (map[k][5][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][7][0] - min_x) * scale_x), (float) (base_y + (map[k][7][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][8][0] - min_x) * scale_x), (float) (base_y + (map[k][8][1] - min_y) * scale_y)); } else { path.moveTo((float) (base_x + (map[k][0][0] - min_x) * scale_x), (float) (base_y + (map[k][0][1] - min_y) * scale_y)); for (int i = 1; i < map[k].length; i++) { path.lineTo((float) (base_x + (map[k][i][0] - min_x) * scale_x), (float) (base_y + (map[k][i][1] - min_y) * scale_y)); } if (map_other.length>9){ path.moveTo((float) (base_x + (map_other[0][0] - min_x) * scale_x), (float) (base_y + (map_other[0][1] - min_y) * scale_y)); for (int i = 1; i < map[k].length; i++) { path.lineTo((float) (base_x + (map_other[i][0] - min_x) * scale_x), (float) (base_y + (map_other[i][1] - min_y) * scale_y)); } } for (int i = 0, j = 0; i < map[k].length && j < map_other.length; i++, j++) { path.moveTo((float) (base_x + (map[k][i][0] - min_x) * scale_x), (float) (base_y + (map[k][i][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map_other[j][0] - min_x) * scale_x), (float) (base_y + (map_other[j][1] - min_y) * scale_y)); } } } canvas2.drawPath(path, paint); 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.i(TAG,String.format("car[%d][0]=%f,to X =%f,car[%d][1]=%f,to Y=%f",i,car[body.get(i)][0], (float) (base_x + (car[body.get(i)][0] - min_x) * scale_x),i,car[body.get(i)][1],(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); paint.setStyle(Paint.Style.FILL_AND_STROKE); canvas3.drawColor(Color.RED); if (osdHeading != null) { Path pathText = new Path(); pathText.moveTo(10, 170); pathText.lineTo(700, 170); canvas3.drawTextOnPath(osdHeading, pathText, 0, 0, paint);//逆时针生成 } if (osdMoveDirect != null) { Path pathText = new Path(); pathText.moveTo(10, 30); pathText.lineTo(700, 30); canvas3.drawTextOnPath(osdMoveDirect, pathText, 0, 0, paint);//逆时针生成 } if (osdRtkSpeed != null) { Path pathText = new Path(); pathText.moveTo(10, 240); pathText.lineTo(700, 240); canvas3.drawTextOnPath(osdRtkSpeed, pathText, 0, 0, paint);//逆时针生成 } if (osdQf != null) { Path pathText = new Path(); pathText.moveTo(10, 120); pathText.lineTo(700, 120); canvas3.drawTextOnPath(osdQf, pathText, 0, 0, paint);//逆时针生成 } { BigDecimal bd = new BigDecimal(gpsSpeed); bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP); Path pathText = new Path(); pathText.moveTo(10, 70); pathText.lineTo(700, 70); canvas3.drawTextOnPath("GPS速度:" + bd, pathText, 0, 0, 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); } // } // }); } /* public void DrawMapAll(List mainAnt, final double[][][] map, final double[][] car, Listbody, List tire) { if (canvas2 == null || bmp == null) { return; } long scale_x,scale_y; // _mActivity.runOnUiThread(new Runnable() { // @Override // public void run() { double base_x = 300, base_y = 20; double max_x = 0, min_x = 0, max_y = 0, min_y = 0; // Log.d(TAG, "DrawMap map size " + map.length + " car size " + car.length); for (int x = 0; x < map.length; x++) { for (int j = 0; j < map[x].length; j++) { if (x == 0&&j==0) { max_x = map[0][0][0]; min_x = map[0][0][0]; max_y = map[0][0][1]; min_y = map[0][0][1]; } else { // Log.i(TAG,String.format("map[%d][%d][0]=%f",x,j,map[x][j][0])); // Log.i(TAG,String.format("map[%d][%d][1]=%f",x,j,map[x][j][1])); // Log.i(TAG,String.format("max_x=%f,max_y=%f,min_x=%f,min_y=%f",max_x,max_y,min_x,min_y)); if (max_x < map[x][j][0]) { max_x = map[x][j][0]; } if (min_x > map[x][j][0]) { min_x = map[x][j][0]; } if (max_y < map[x][j][1]) { max_y = map[x][j][1]; } if (min_y > map[x][j][1]) { min_y = map[x][j][1]; } } } } // Log.d(TAG, "all 地图DrawMap max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); if (map_other.length>9){ for (int i = 0; i < map_other.length; i++) { if (max_x < map_other[i][0]) { max_x = map_other[i][0]; } if (min_x > map_other[i][0]) { min_x = map_other[i][0]; } if (max_y < map_other[i][1]) { max_y = map_other[i][1]; } if (min_y > map_other[i][1]) { min_y = map_other[i][1]; } } } // Log.d(TAG, "DrawMap另外一条曲线 max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); for (int i = 0; i < car.length; i++) { if (max_x < car[i][0]) { max_x = car[i][0]; } if (min_x > car[i][0]) { min_x = car[i][0]; } if (max_y < car[i][1]) { max_y = car[i][1]; } if (min_y > car[i][1]) { min_y = car[i][1]; } } // Log.d(TAG, "DrawMapAll 和车比max_x " + max_x + " max_y " + max_y + " min_x " + min_x + " min_y " + min_y); scale_x = Math.round((screen_width - base_x - 10) / (max_x - min_x)); scale_y = Math.round((screen_height - base_y - 10) / (max_y - min_y)); if (scale_x >= scale_y) { scale_x = scale_y; } else { scale_y = scale_x; } // Log.d(TAG, "DrawMapAll scale_x " + scale_x + " scale_y " + scale_y); canvas2.drawColor(Color.WHITE); if (paint == null || canvas2==null){ return; } paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setColor(Color.BLUE); canvas2.drawCircle((float) (base_x + (mainAnt.get(0) - min_x) * scale_x), (float) (base_y + (0 - mainAnt.get(1) - min_y) * scale_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(); for (int k = 0; k < map.length; k++) { Log.i(TAG, String.format("all DrawMap: map[%d].length=[%d]",k,map[k].length)); if (map[k].length < 9) { // Log.d(TAG, String.format("map[%d][0][0]=%f,X=%f",k,map[k][0][0], (float) (base_x + (map[k][0][0] - min_x) * scale_x)) + String.format("map[%d][0][1]=%f,Y=%f",k,map[k][0][1], (float) (base_y + (map[k][0][1] - min_y) * scale_y))); path.moveTo((float) (base_x + (map[k][0][0] - min_x) * scale_x), (float) (base_y + (map[k][0][1] - min_y) * scale_y)); for (int i = 1; i < map[k].length; i++) { // Log.i(TAG,String.format("map[%d][%d][0]=%f,to X=%f",k,i,map[k][i][0],(float) (base_x + (map[k][i][0] - min_x) * scale_x))); // Log.i(TAG,String.format("map[%d][%d][1]=%f,to Y=%f",k,i,map[k][i][1],(float) (base_y + (map[k][i][1] - min_y) * scale_y))); path.lineTo((float) (base_x + (map[k][i][0] - min_x) * scale_x), (float) (base_y + (map[k][i][1] - min_y) * scale_y)); } path.close(); } else if (map[k].length == 9) { path.moveTo((float) (base_x + (map[k][0][0] - min_x) * scale_x), (float) (base_y + (map[k][0][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][8][0] - min_x) * scale_x), (float) (base_y + (map[k][8][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][2][0] - min_x) * scale_x), (float) (base_y + (map[k][2][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][1][0] - min_x) * scale_x), (float) (base_y + (map[k][1][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][3][0] - min_x) * scale_x), (float) (base_y + (map[k][3][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][4][0] - min_x) * scale_x), (float) (base_y + (map[k][4][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][6][0] - min_x) * scale_x), (float) (base_y + (map[k][6][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][5][0] - min_x) * scale_x), (float) (base_y + (map[k][5][1] - min_y) * scale_y)); path.moveTo((float) (base_x + (map[k][7][0] - min_x) * scale_x), (float) (base_y + (map[k][7][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map[k][8][0] - min_x) * scale_x), (float) (base_y + (map[k][8][1] - min_y) * scale_y)); } else { path.moveTo((float) (base_x + (map[k][0][0] - min_x) * scale_x), (float) (base_y + (map[k][0][1] - min_y) * scale_y)); for (int i = 1; i < map[k].length; i++) { path.lineTo((float) (base_x + (map[k][i][0] - min_x) * scale_x), (float) (base_y + (map[k][i][1] - min_y) * scale_y)); } if (map_other.length>9){ path.moveTo((float) (base_x + (map_other[0][0] - min_x) * scale_x), (float) (base_y + (map_other[0][1] - min_y) * scale_y)); for (int i = 1; i < map[k].length; i++) { path.lineTo((float) (base_x + (map_other[i][0] - min_x) * scale_x), (float) (base_y + (map_other[i][1] - min_y) * scale_y)); } } for (int i = 0, j = 0; i < map[k].length && j < map_other.length; i++, j++) { path.moveTo((float) (base_x + (map[k][i][0] - min_x) * scale_x), (float) (base_y + (map[k][i][1] - min_y) * scale_y)); path.lineTo((float) (base_x + (map_other[j][0] - min_x) * scale_x), (float) (base_y + (map_other[j][1] - min_y) * scale_y)); } } } canvas2.drawPath(path, paint); 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.i(TAG,String.format("car[%d][0]=%f,to X =%f,car[%d][1]=%f,to Y=%f",i,car[body.get(i)][0], (float) (base_x + (car[body.get(i)][0] - min_x) * scale_x),i,car[body.get(i)][1],(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); paint.setStyle(Paint.Style.FILL_AND_STROKE); if (osdHeading != null) { Path pathText = new Path(); pathText.moveTo(10, 170); pathText.lineTo(700, 170); canvas2.drawTextOnPath(osdHeading, pathText, 0, 0, paint);//逆时针生成 } if (osdMoveDirect != null) { Path pathText = new Path(); pathText.moveTo(10, 30); pathText.lineTo(700, 30); canvas2.drawTextOnPath(osdMoveDirect, pathText, 0, 0, paint);//逆时针生成 } if (osdRtkSpeed != null) { Path pathText = new Path(); pathText.moveTo(10, 240); pathText.lineTo(700, 240); canvas2.drawTextOnPath(osdRtkSpeed, pathText, 0, 0, paint);//逆时针生成 } if (osdQf != null) { Path pathText = new Path(); pathText.moveTo(10, 120); pathText.lineTo(700, 120); canvas2.drawTextOnPath(osdQf, pathText, 0, 0, paint);//逆时针生成 } { BigDecimal bd = new BigDecimal(gpsSpeed); bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP); Path pathText = new Path(); pathText.moveTo(10, 70); pathText.lineTo(700, 70); canvas2.drawTextOnPath("GPS速度:" + bd, pathText, 0, 0, paint);//逆时针生成 } // 提交画布 Canvas canvas = holder.lockCanvas(); if (canvas != null){ canvas.drawBitmap(bmp, 0, 0, paint); holder.unlockCanvasAndPost(canvas); } // } // }); } */ int line = 0; int map_line = 0; int map_line_other = 0; int sanwei = 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, final int msgCode, int resultCode, final Object 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; @Override public void run() { while (flag){ MessageRemoteService messageRemoteService = (MessageRemoteService) queue.peek(); if (messageRemoteService == null){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } messageRemoteService = (MessageRemoteService) queue.poll(); if (messageRemoteService != null){ this.cmd = messageRemoteService.msgCode; this.json = messageRemoteService.json; MyLog.i(TAG, json); RealTimeCarPos timeCarPos = gson.fromJson((String) json, RealTimeCarPos.class); List points = timeCarPos.getPoint(); switch (timeCarPos.getMove()){ case 0: osdMoveDirect = "停车"; break; case 1: osdMoveDirect = "前进"; break; case -1: osdMoveDirect = "后退"; break; } osdHeading="方向角"+String.valueOf(timeCarPos.getHeading()); BigDecimal bd = new BigDecimal(timeCarPos.getSpeed()); bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP); osdRtkSpeed = "计算速度:" + bd; osdQf = "QF:" + String.valueOf(timeCarPos.getQf()); line = 0; map_line = 0; map_line_other = 0; sanwei = 0; car = new double[points.size()/2][2]; for (int i = 0; i < points.size(); i++) { if ((i % 2) == 0) { car[line][0] = points.get(i); } else { double value = points.get(i); car[line][1] = value; line++; } } map_id = timeCarPos.getMap_id(); List mainAnt = timeCarPos.getMain_ant(); List tire1 = timeCarPos.getLeft_front_tire(); List tire2 = timeCarPos.getRight_front_tire(); List tire3 = timeCarPos.getLeft_rear_tire(); List tire4 = timeCarPos.getRight_rear_tire(); List body = timeCarPos.getBody(); List tire = new ArrayList<>(); double yaw = timeCarPos.getHeading(); tire.add(tire1.get(0)); tire.add(tire2.get(0)); tire.add(tire3.get(0)); tire.add(tire4.get(0)); // 1 - 倒库 // 2 - 坡起 // 3 - 侧方停车 // 4 - 曲线行驶 // 5 - 直角转弯 StringBuffer buffer=null; List examMaps; String newmap = null; String mapPath = ExamPlatformData.getInstance().getMapPath(); if (!TextUtils.isEmpty(mapPath)){ MyLog.i("调用更新Map路径后的地图"+mapPath); byte[] fileContent = FileUtil.readFile(mapPath); if (fileContent != null){ newmap= new String(fileContent); }else{ MyLog.i(String.format("文件:%s不存在",mapPath)); } }else{ buffer = FileUtil.readAssetTxtFile(_mActivity,Constant.MAP); } Type type = new TypeToken>(){}.getType(); if (buffer != null){ examMaps= gson.fromJson(buffer.toString().trim(), type); }else{ examMaps = gson.fromJson(newmap.trim(),type); } map_id = -1; if (map_id == -1){ allMap =new double[examMaps.size()][][]; for (int i = 0; i < examMaps.size(); i++) { ExamMap examMap = examMaps.get(i); List pointBeanList = examMap.getPoint(); for(int j=0; j xys = pointBeanList.get(j).getXy(); if(j==0){ map = new double[xys.size()/2][2]; allMap[sanwei] = new double[xys.size()/2][2]; map_line = 0; for (int k=0; k pointBeanList = examMap.getPoint(); for(int j=0; j xys = pointBeanList.get(j).getXy(); if(j==0){ map = new double[xys.size()/2][2]; for (int k=0; k