Explorar o código

封装调整自定义的mapview使用的layer

Frankensteinly hai 10 meses
pai
achega
83bb4e8a10

+ 9 - 0
app/src/main/java/com/grkj/iscs/model/bo/CustomPoint.kt

@@ -0,0 +1,9 @@
+package com.grkj.iscs.model.bo
+
+import android.graphics.PointF
+
+data class CustomPoint(
+    var pos: PointF?,
+    var name: String = "",
+    var status: Int = 1
+)

+ 210 - 0
app/src/main/java/com/grkj/iscs/view/widget/CustomMarkLayer.java

@@ -0,0 +1,210 @@
+package com.grkj.iscs.view.widget;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Matrix;
+import android.graphics.Paint;
+import android.graphics.PointF;
+import android.util.Pair;
+import android.view.MotionEvent;
+
+import com.grkj.iscs.R;
+import com.grkj.iscs.model.bo.CustomPoint;
+import com.onlylemi.mapview.library.MapView;
+import com.onlylemi.mapview.library.layer.MapBaseLayer;
+import com.onlylemi.mapview.library.utils.MapMath;
+
+import java.util.List;
+
+public class CustomMarkLayer extends MapBaseLayer {
+    private List<CustomPoint> pointList;
+    private MarkIsClickListener listener;
+    private Bitmap btn1;
+    private Bitmap btn2;
+    private Bitmap btn3;
+    private Bitmap bmpMarkTouch;
+    private float radiusMark;
+    private boolean isClickMark;
+    private int num;
+    private Paint paint;
+    private int btnIndex;
+    private float currentZoom;
+    private float currentDegree;
+
+    public CustomMarkLayer(MapView mapView) {
+        this(mapView, null);
+    }
+
+    public CustomMarkLayer(MapView mapView, List<CustomPoint> list) {
+        super(mapView);
+        isClickMark = false;
+        num = -1;
+        btnIndex = -1;
+        pointList = list;
+        initLayer();
+    }
+
+    private void initLayer() {
+        radiusMark = setValue(10.0F);
+        btn1 = BitmapFactory.decodeResource(mapView.getResources(), R.mipmap.icon1);
+        btn2 = BitmapFactory.decodeResource(mapView.getResources(), R.mipmap.icon2);
+        btn3 = BitmapFactory.decodeResource(mapView.getResources(), R.mipmap.icon3);
+        bmpMarkTouch = BitmapFactory.decodeResource(mapView.getResources(), R.mipmap.icon2);
+        paint = new Paint();
+        paint.setAntiAlias(true);
+        paint.setStyle(Paint.Style.FILL_AND_STROKE);
+    }
+
+    public void onTouch(MotionEvent event) {
+        if (pointList != null && !pointList.isEmpty()) {
+            float[] goal = mapView.convertMapXYToScreenXY(event.getX(), event.getY());
+
+            for (int i = 0; i < pointList.size(); ++i) {
+                // 按钮1
+//                if (MapMath.getDistanceBetweenTwoPoints(goal[0], goal[1],
+//                        pointList.get(i).getPos().x,
+//                        pointList.get(i).getPos().y
+//                ) <= btn1.getWidth() / 2 / currentZoom) {
+//                    num = i;
+//                    btnIndex = 1;
+//                    isClickMark = true;
+//                    break;
+//                }
+
+                // 按钮2
+                if (MapMath.getDistanceBetweenTwoPoints(
+                        goal[0], goal[1],
+//                        pointList.get(i).getPos().x - btn2.getWidth() / 2 / currentZoom,
+                        rotatePoint(pointList.get(i).getPos().x - btn2.getWidth() / 2 / currentZoom, pointList.get(i).getPos().y - btn2.getHeight() / currentZoom, pointList.get(i).getPos().x, pointList.get(i).getPos().y, currentDegree).first,
+                        rotatePoint(pointList.get(i).getPos().x - btn2.getWidth() / 2 / currentZoom, pointList.get(i).getPos().y - btn2.getHeight() / currentZoom, pointList.get(i).getPos().x, pointList.get(i).getPos().y, currentDegree).second
+                ) <= btn2.getWidth() / 2 / currentZoom) {
+                    num = i;
+                    btnIndex = 2;
+                    isClickMark = true;
+                    break;
+                }
+
+                // 按钮3
+                if (MapMath.getDistanceBetweenTwoPoints(goal[0], goal[1],
+                        rotatePoint(pointList.get(i).getPos().x + btn3.getWidth() / 2 / currentZoom, pointList.get(i).getPos().y - btn3.getHeight() / currentZoom, pointList.get(i).getPos().x, pointList.get(i).getPos().y, currentDegree).first,
+                        rotatePoint(pointList.get(i).getPos().x + btn3.getWidth() / 2 / currentZoom, pointList.get(i).getPos().y - btn3.getHeight() / currentZoom, pointList.get(i).getPos().x, pointList.get(i).getPos().y, currentDegree).second
+                ) <= btn3.getWidth() / 2 / currentZoom) {
+                    num = i;
+                    btnIndex = 3;
+                    isClickMark = true;
+                    break;
+                }
+
+                if (i == pointList.size() - 1) {
+                    isClickMark = false;
+                }
+            }
+
+            if (listener != null && isClickMark) {
+                listener.markIsClick(num, btnIndex);
+                mapView.refresh();
+            }
+        }
+
+    }
+
+    public void draw(Canvas canvas, Matrix currentMatrix, float currentZoom, float currentRotateDegrees) {
+        this.currentZoom = currentZoom;
+        currentDegree = 360 - currentRotateDegrees;
+        if (isVisible && pointList != null) {
+            canvas.save();
+            if (!pointList.isEmpty()) {
+                for (int i = 0; i < pointList.size(); ++i) {
+                    PointF mark = pointList.get(i).getPos();
+                    float[] goal = new float[]{mark.x, mark.y};
+                    currentMatrix.mapPoints(goal);
+
+                    // TODO 文字背景
+                    float width = paint.measureText(pointList.get(i).getName());
+                    paint.setColor(Color.parseColor("#FFBB86FC"));
+                    paint.setStyle(Paint.Style.FILL);
+                    paint.setStrokeWidth(1.0F);
+                    // 圆角矩形
+                    canvas.drawRoundRect(goal[0] - width / 2, goal[1] - radiusMark / 2.0F, goal[0] + width / 2, goal[1] + radiusMark / 2.0F, 10f, 10f, paint);
+
+
+                    paint.setColor(-16777216);
+                    paint.setTextSize(radiusMark);
+                    if ((double) mapView.getCurrentZoom() > 1.0 && pointList != null) {
+//                        canvas.drawText(pointList.get(i).getName(), goal[0] - radiusMark, goal[1] - radiusMark / 2.0F, paint);
+                        canvas.drawText(pointList.get(i).getName(), goal[0] - width / 2, goal[1] + radiusMark / 3.0F, paint);
+                    }
+
+                    // TODO 此处不需要按钮,用文字替换
+//                    if (pointList.get(i).getStatus() < 2) {
+//                        canvas.drawBitmap(btn1, goal[0] - (float)(btn1.getWidth() / 2), goal[1] - (float)(btn1.getHeight() / 2), paint);
+//                    }
+                    // TODO 根据状态绘制按钮
+                    if (pointList.get(i).getStatus() == 2) {
+                        canvas.drawBitmap(btn2, goal[0] - (float) (btn2.getWidth()), goal[1] - (float) (btn2.getHeight() / 2 * 3), paint);
+                    }
+                    // TODO 根据状态绘制按钮
+                    if (pointList.get(i).getStatus() == 3) {
+                        canvas.drawBitmap(btn3, goal[0], goal[1] - (float) (btn3.getHeight() / 2 * 3), paint);
+                    }
+                    canvas.drawCircle(goal[0], goal[1], 2, paint);
+                    if (i == num && isClickMark) {
+                        canvas.drawBitmap(bmpMarkTouch, goal[0] - (float) (bmpMarkTouch.getWidth() / 2), goal[1] - (float) bmpMarkTouch.getHeight(), paint);
+                    }
+                }
+            }
+
+            canvas.restore();
+        }
+
+    }
+
+    public Pair<Float, Float> rotatePoint(float oriX, float oriY, float desX, float desY, float rotateDegrees) {
+        // 将度数转换为弧度
+        double theta = Math.toRadians(rotateDegrees);
+
+        // 计算旋转后的坐标
+        double A_new_x = (oriX - desX) * Math.cos(theta) - (oriY - desY) * Math.sin(theta) + desX;
+        double A_new_y = (oriX - desX) * Math.sin(theta) + (oriY - desY) * Math.cos(theta) + desY;
+
+        return new Pair<>((float) A_new_x, (float) A_new_y);
+    }
+
+    public int getNum() {
+        return num;
+    }
+
+    public void setNum(int num) {
+        num = num;
+    }
+
+//    public List<PointF> getMarks() {
+//        return marks;
+//    }
+//
+//    public void setMarks(List<PointF> marks) {
+//        this.marks = marks;
+//    }
+//
+//    public List<String> getMarksName() {
+//        return marksName;
+//    }
+//
+//    public void setMarksName(List<String> marksName) {
+//        this.marksName = marksName;
+//    }
+
+    public boolean isClickMark() {
+        return isClickMark;
+    }
+
+    public void setMarkIsClickListener(MarkIsClickListener listener) {
+        this.listener = listener;
+    }
+
+    public interface MarkIsClickListener {
+        void markIsClick(int index, int btnIndex);
+    }
+}

BIN=BIN
app/src/main/res/mipmap/icon1.png


BIN=BIN
app/src/main/res/mipmap/icon2.png


BIN=BIN
app/src/main/res/mipmap/icon3.png