如何在图像上添加多个图钉标记

时间:2016-08-17 15:29:46

标签: android image marker image-zoom

我想将标记放在具有以下功能的图像上

- 在图像上动态添加多个标记,并在图像上使用缩放和滚动功能。

- 添加到图像上后标记上的链接动画。

-Marker可以点击并拖动和放大放在图像上。

-Marker的x& y图像上的位置与其他设备同步

我使用过https://github.com/davemorrissey/subsampling-scale-image-view

Java代码:

公共类PinMarkerView扩展SubsamplingScaleImageView实现了View.OnTouchListener {

private PointF sPin;
private Bitmap pin;
private int resId;
public float vX;
public float vY;

private PointF vPrevious;
private PointF vStart;
private boolean drawing = false;

private boolean markerTouch = false;
private int strokeWidth;

private ArrayList<MapPins> allPins = new ArrayList<MapPins>();
private ArrayList<DrawPins> drawnPins = new ArrayList<DrawPins>();


public PinView(Context context) {
    this(context, null);
}

public PinView(Context context, AttributeSet attr) {
    super(context, attr);
    //  initialise();
}

public void setPin(PointF sPin, @DrawableRes int resid, String extras) {

    MapPins mapPins = new MapPins();
    mapPins.setPointF(sPin);
    mapPins.setResId(resid);
    mapPins.setExtrasData(extras);
    mapPins.setX(sPin.x);
    mapPins.setY(sPin.y);
    allPins.add(mapPins);

    //this.sPin = sPin;
    // this.resId = resid;
    initialise();
    invalidate();


}

public PointF getPin() {
    return sPin;
}

public Bitmap getMarker() {
    return pin;
}

private void initialise() {
    setOnTouchListener(this);
    float density = getResources().getDisplayMetrics().densityDpi;

    strokeWidth = (int) (density / 60f);

    for (int i = 0; i < allPins.size(); i++) {
        MapPins mapPins = allPins.get(i);
        Bitmap localpin = BitmapFactory.decodeResource(this.getResources(), mapPins.getResId());
        float w = (density / 100f) * localpin.getWidth();
        float h = (density / 100f) * localpin.getHeight();
        //pin = Bitmap.createScaledBitmap(pin, (int) w, (int) h, true);
        pin = Bitmap.createScaledBitmap(localpin, localpin.getWidth(), localpin.getHeight(), true);
        mapPins.setCreatedBitmap(pin);
        allPins.set(i, mapPins);
        //-------

    }

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // Don't draw pin before image is ready so it doesn't move around during setup.
    if (!isReady()) {
        return;
    }

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    for (int i = 0; i < allPins.size(); i++) {
        MapPins mapPins = allPins.get(i);
        if (mapPins.getPointF() != null && mapPins.getCreatedBitmap() != null) {
            PointF vPin = sourceToViewCoord(mapPins.getPointF());
            vX = vPin.x - (mapPins.getCreatedBitmap().getWidth() / 2);
            vY = vPin.y - mapPins.getCreatedBitmap().getHeight();
            canvas.drawBitmap(mapPins.getCreatedBitmap(), vX, vY, paint);

        }
    }
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    setMaxScale(5f);
    return false;
}


@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

  /*  if (isZoomEnabled()) {
        return super.onTouchEvent(event);
    }*/
    boolean consumed = false;
    int touchCount = event.getPointerCount();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

        case MotionEvent.ACTION_POINTER_1_DOWN:
            vStart = new PointF(event.getX(), event.getY());
            vPrevious = new PointF(event.getX(), event.getY());
            getPinIdByPoint(vStart, viewToSourceCoord(event.getX(), event.getY()));
            handleActionDown((int) event.getX(), (int) event.getY());

            break;
        case MotionEvent.ACTION_POINTER_2_DOWN:
            // Abort any current drawing, user is zooming
            vStart = null;
            vPrevious = null;
            break;
        case MotionEvent.ACTION_MOVE:
            if (markerTouch) {
                setPanEnabled(false);
                PointF sCurrentF = viewToSourceCoord(event.getX(), event.getY());
                PointF sCurrent = new PointF(sCurrentF.x, sCurrentF.y);
                PointF sStart = vStart == null ? null : new PointF(viewToSourceCoord(vStart).x, viewToSourceCoord(vStart).y);
                if (touchCount == 1 && vStart != null) {
                    float vDX = Math.abs(event.getX() - vPrevious.x);
                    float vDY = Math.abs(event.getY() - vPrevious.y);
                    if (vDX >= strokeWidth * 5 || vDY >= strokeWidth * 5) {
                        if (sPin == null) {
                            sPin = sStart;
                        }
                        sPin = sCurrent;
                        vPrevious.x = event.getX();
                        vPrevious.y = event.getY();
                        drawing = true;
                    }
                    consumed = true;
                    invalidate();
                } else if (touchCount == 1) {
                    // Consume all one touch drags to prevent odd panning effects handled by the superclass.
                    consumed = true;
                }
            } else {
                return super.onTouchEvent(event);
            }

            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            invalidate();
            setPanEnabled(true);
            drawing = false;
            markerTouch = false;
            vPrevious = null;
            vStart = null;
    }
    // Use parent to handle pinch and two-finger pan.
    return consumed || super.onTouchEvent(event);
}


public void handleActionDown(int eventX, int eventY) {

    if (eventX >= (sPin.x - pin.getWidth()) && (eventX <= (sPin.x + pin.getWidth()))) {

        if (eventY >= (sPin.y - pin.getHeight()) && (sPin.y <= (sPin.y + pin.getHeight()))) {

            markerTouch = true;

        } else {

            markerTouch = false;
        }

    } else {

        markerTouch = false;

    }


}

public int getPinIdByPoint(PointF tappedCoordinate, PointF deeplinkCoordinate) {

    for (int i = allPins.size() - 1; i >= 0; i--) {
        MapPins dPin = allPins.get(i);
        int blockWidth = dPin.getCreatedBitmap().getWidth();
        int blockHeight = dPin.getCreatedBitmap().getHeight();

        int deeplinkX = (int) (deeplinkCoordinate.x - (dPin.getCreatedBitmap().getWidth() / 2));
        int deeplinkY = (int) (deeplinkCoordinate.y - dPin.getCreatedBitmap().getHeight());

        // center coordinate -/+ blockWidth actually sets touchable area to 2x icon size
        if (tappedCoordinate.x >= deeplinkX - blockWidth && tappedCoordinate.x <= deeplinkX + blockWidth &&
                tappedCoordinate.y >= deeplinkY - blockHeight && tappedCoordinate.y <= deeplinkY + blockHeight) {
            sPin = dPin.getPointF();
            pin = dPin.getCreatedBitmap();
            return dPin.getId();
        }
    }
    return -1; //negative no means no pin selected
}

}

目前,我可以添加多个标记,但我面临着标记点击和拖动图像的问题。

0 个答案:

没有答案