| | |
| | | package safeluck.drive.evaluation.fragment; |
| | | |
| | | import android.graphics.Bitmap; |
| | | import android.graphics.Canvas; |
| | | import android.graphics.Color; |
| | | import android.graphics.Paint; |
| | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | | |
| | | import com.anyun.exam.lib.MyLog; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | | |
| | | import me.yokeyword.fragmentation.SupportFragment; |
| | | import safeluck.drive.evaluation.Constant; |
| | | import safeluck.drive.evaluation.bean.ExamMap; |
| | | import safeluck.drive.evaluation.util.FileUtil; |
| | | |
| | | public class RoadDriveMapFragment extends SupportFragment implements SurfaceHolder.Callback { |
| | | |
| | |
| | | private SurfaceHolder mSurfaceHolder; |
| | | private boolean flag = false;//控制绘制线程drawThread |
| | | private Canvas mCanvas; |
| | | private Canvas bitMapCanvas; |
| | | private Paint mPaint; |
| | | private Path mPath; |
| | | private StringBuffer buffer;//存放地图的buffer |
| | | private Gson gson = new Gson(); |
| | | |
| | | private int avaliableWidth,avaliableHeight; |
| | | |
| | | private int x = 50, y = 50, r = 10; // 圆的坐标和半径 |
| | | private int sinx = 0,siny =0 ; |
| | | float [] pts={50,100,100,200,200,300,300,400}; |
| | | |
| | | private ExecutorService generateData = Executors.newSingleThreadExecutor();//产生数据(主要是车辆数据) |
| | | private ExecutorService drawThread = Executors.newSingleThreadExecutor();//绘制线程 |
| | |
| | | avaliableWidth = mSurfaceView.getWidth(); |
| | | Log.i(TAG,"可用宽度="+avaliableWidth+"可用高度="+avaliableHeight); |
| | | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| | | mPaint.setStyle(Paint.Style.STROKE); |
| | | mPaint.setStrokeWidth(1.5f); |
| | | mPath = new Path(); |
| | | |
| | | int widhtpixels =getResources().getDisplayMetrics().widthPixels; |
| | | int heightpixels =getResources().getDisplayMetrics().heightPixels; |
| | | Log.i(TAG,"widhtpixels="+widhtpixels+" heig="+heightpixels); |
| | | return mSurfaceView; |
| | | } |
| | | |
| | | @Override |
| | | public void surfaceCreated(SurfaceHolder holder) { |
| | | // mCanvas = holder.lockCanvas(); |
| | | // int screen_width = mCanvas.getWidth(); |
| | | // int screen_height = mCanvas.getHeight(); |
| | | // Bitmap bmp = Bitmap.createBitmap(screen_width, screen_height, Bitmap.Config.ARGB_8888); |
| | | // bitMapCanvas = new Canvas(bmp); |
| | | // bitMapCanvas.drawColor(Color.WHITE); |
| | | // |
| | | // mPaint = new Paint(); |
| | | // |
| | | // mPaint.setTextSize(30); |
| | | // mPaint.setColor(Color.BLACK); |
| | | // mPaint.setStrokeWidth(1.5f); |
| | | // mPaint.setAntiAlias(true); |
| | | // mPaint.setStyle(Paint.Style.STROKE); |
| | | // |
| | | // mCanvas.drawBitmap(bmp,0,0,mPaint); |
| | | // holder.unlockCanvasAndPost(mCanvas); |
| | | |
| | | |
| | | flag = true; |
| | | drawThread.execute(new DrawRunnable()); |
| | | } |
| | | |
| | |
| | | flag = false; |
| | | } |
| | | |
| | | |
| | | List<ExamMap> examMaps = new ArrayList<>(); |
| | | /** |
| | | * 主要是这个方法,进行坐标点计算,使用mPath画图,可能要用到Canvas |
| | | * 最好只使用path |
| | | */ |
| | | private void calculate() { |
| | | mPath.addCircle(x,y,r, Path.Direction.CCW); |
| | | int map_id = 863; |
| | | int map_line = 0; |
| | | double map[][]={}; |
| | | |
| | | if (buffer == null){ |
| | | |
| | | buffer = FileUtil.readAssetTxtFile(_mActivity, Constant.ROAD_MAP); |
| | | Log.i(TAG,"ditu="+buffer.toString().trim()); |
| | | } |
| | | Type type = new TypeToken<List<ExamMap>>(){}.getType(); |
| | | if (buffer != null){ |
| | | 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 j=0; j<pointBeanList.size();j++){ |
| | | List<Double> xys = pointBeanList.get(j).getXy(); |
| | | if(j==0){ |
| | | map = new double[xys.size()/2][2]; |
| | | |
| | | |
| | | |
| | | for (int k=0; k<xys.size();k++){ |
| | | if ((k % 2) == 0) { |
| | | map[map_line][0] = xys.get(k); |
| | | Log.i(TAG, "onCEvent: map新值=" + map[map_line][0] + " 位置:" + k); |
| | | } else { |
| | | double value = 0-xys.get(k); |
| | | |
| | | |
| | | map[map_line][1] = value; |
| | | map_line++; |
| | | } |
| | | } |
| | | }else{ |
| | | MyLog.i(TAG,"暂时支持曲线"); |
| | | break; |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | onDrawMap(map); |
| | | // mPaint.reset(); |
| | | // mPaint.setStyle(Paint.Style.FILL); |
| | | // mPaint.setColor(Color.RED); |
| | | // mPath.addCircle(x,y,r, Path.Direction.CCW); |
| | | // |
| | | // mCanvas.drawPath(mPath,mPaint); |
| | | // |
| | | // |
| | | // mPaint.reset(); |
| | | // mPaint.setStyle(Paint.Style.FILL); |
| | | // mPaint.setColor(Color.GREEN); |
| | | // mCanvas.drawCircle(x+100,y+100,70,mPaint); |
| | | // |
| | | // |
| | | // mPaint.reset(); |
| | | // mPath = new Path(); |
| | | // mPaint.setColor(Color.GREEN); |
| | | // mPaint.setStyle(Paint.Style.STROKE); |
| | | // mPaint.setStrokeWidth(1.5f); |
| | | // |
| | | // mPath.moveTo(pts[0],pts[1]); |
| | | // mPath.lineTo(pts[2],pts[3]); |
| | | // mCanvas.drawPath(mPath,mPaint); |
| | | |
| | | |
| | | |
| | | if (mCanvas != null){ |
| | | //释放canvas对象并提交画布 |
| | | mSurfaceHolder.unlockCanvasAndPost(mCanvas); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | private void onDrawMap(final double map[][]){ |
| | | 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 " ); |
| | | |
| | | 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]; |
| | | } |
| | | } |
| | | } |
| | | long scale_x = Math.round((mCanvas.getWidth() - base_x - 10) / (max_x - min_x)); |
| | | long scale_y = Math.round((mCanvas.getHeight() - base_y - 10) / (max_y - min_y)); |
| | | |
| | | if (scale_x >= scale_y) { |
| | | scale_x = scale_y; |
| | | } else { |
| | | scale_y = scale_x; |
| | | } |
| | | |
| | | mPaint.reset(); |
| | | mPaint.setAntiAlias(true); |
| | | mPaint.setColor(Color.BLACK); |
| | | mPaint.setStyle(Paint.Style.STROKE); |
| | | mPaint.setStrokeWidth(1.5f); |
| | | |
| | | |
| | | |
| | | 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))); |
| | | mPath.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)); |
| | | mPath.lineTo((float) (base_x + (map[i][0] - min_x) * scale_x), (float) (base_y + (map[i][1] - min_y) * scale_y)); |
| | | } |
| | | mPath.close(); |
| | | mCanvas.drawPath(mPath,mPaint); |
| | | } |
| | | mPaint.reset(); |
| | | mPath = new Path(); |
| | | mPaint.setStyle(Paint.Style.STROKE); |
| | | mPaint.setStrokeWidth(1.5f); |
| | | mPaint.setColor(Color.RED); |
| | | mPath.moveTo(300,150); |
| | | mPath.lineTo(1015,150); |
| | | |
| | | mCanvas.drawPath(mPath,mPaint); |
| | | } |
| | | |
| | | /** |
| | | * 绘制 |
| | |
| | | mCanvas = mSurfaceHolder.lockCanvas(); |
| | | //绘制背景 |
| | | mCanvas.drawColor(Color.WHITE); |
| | | Log.i(TAG,"width="+mCanvas.getWidth()+"height:"+mCanvas.getHeight()); |
| | | //绘制路径 |
| | | mCanvas.drawPath(mPath, mPaint); |
| | | }catch (Exception e){ |
| | | |
| | | }finally { |
| | | if (mCanvas != null){ |
| | | //释放canvas对象并提交画布 |
| | | mSurfaceHolder.unlockCanvasAndPost(mCanvas); |
| | | } |
| | | // if (mCanvas != null){ |
| | | // mCanvas.restore(); |
| | | // //释放canvas对象并提交画布 |
| | | // mSurfaceHolder.unlockCanvasAndPost(mCanvas); |
| | | // } |
| | | } |
| | | |
| | | } |