无法使用Osmdroid实现onTouchEvent(拖放)

发布于 2024-12-13 06:27:35 字数 3380 浏览 3 评论 0原文

我一直在尝试实现 OnTouchEvent,以便当用户移动(拖放)覆盖项时我可以做出反应。

我在 Osmdroid 网站上发现了类似的内容:http://code.google。 com/p/osmdroid/issues/detail?id=225。我使用那里的代码作为参考,但仍然不适合我,然后我的应用程序在到达 hitTest 时崩溃。

有什么建议吗?谢谢。

public boolean onTouchEvent(MotionEvent event, MapView mapView) {

    final int action = event.getAction();
    final int x = (int) event.getX();
    final int y = (int) event.getY();

    final Projection pj = mapView.getProjection();

    boolean result = false;
    //Object TAG;
    //Log.d(TAG, "onTouchEvent entered");

    System.out.println("onTouchEvent!");        
    Point p = new Point(0,0);
    Point t = new Point(0,0);

    //System.out.print(MotionEvent.ACTION_DOWN);
    //System.out.print(action);

    if (action == MotionEvent.ACTION_DOWN) {

        System.out.println("Action Down!");

        for (OverlayItem item : mOverlays) {

            // Create a new GeoPoint from pixel coordinates (x, y, pointReuse):
            //pj.fromMapPixels(x, y, t);

            // Convert the given GeoPoint to onscreen pixel coordinates (GeoPoint, pointOut):
            pj.toPixels(item.getPoint(), p);

            //System.out.println(t.x);
            //System.out.println(p.x);

            //System.out.println(t.y);
            //System.out.println(p.y);

            defaultMarker = item.getDrawable();

            if (hitTest(item, defaultMarker, x - p.x, y - p.y)) {
                System.out.println("Action Down -> IF!");
                result = true;
                inDrag = item;
                mOverlays.remove(inDrag);
                populate();

                xDragTouchOffset = 0;
                yDragTouchOffset = 0;

                setDragImagePosition(x, y);
                dragImage.setVisibility(View.VISIBLE);

                xDragTouchOffset = t.x - p.x;
                yDragTouchOffset = t.y - p.y;

                break;
            }
        }

    } 

    else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
        //dragImage.setVisibility(View.VISIBLE);
        setDragImagePosition(x, y);
        System.out.println("Action Move!");
        result = true;
    } 

    else if (action == MotionEvent.ACTION_UP && inDrag != null) {
        dragImage.setVisibility(View.GONE);

        GeoPoint pt = (GeoPoint) pj.fromPixels(x - xDragTouchOffset, y - yDragTouchOffset);
        OverlayItem toDrop = new OverlayItem(inDrag.getTitle(),
                inDrag.getSnippet(), pt);

        mOverlays.add(toDrop);
        populate();

        inDrag = null;
        result = true; 

        pj.fromMapPixels(x, y, t);

        if((t.x - p.x) == xDragTouchOffset && (t.y - p.y) == yDragTouchOffset) {
            System.out.println ("Do something here if desired because we didn't move item " + toDrop.getTitle() );
        }

        System.out.println("Action Up!");

    }
    System.out.print(inDrag);
    return (result || super.onTouchEvent(event, mapView));
}

private void setDragImagePosition(int x, int y) {
    RelativeLayout.LayoutParams lp=
            (RelativeLayout.LayoutParams)dragImage.getLayoutParams();
    lp.setMargins(x-xDragImageOffset-xDragTouchOffset,
            y-yDragImageOffset-yDragTouchOffset, 0, 0);
    dragImage.setLayoutParams(lp);
    }

I had been trying to implement OnTouchEvent so I can react when an user move (drag & drop) an overlayitem.

I found something similar on the Osmdroid site: http://code.google.com/p/osmdroid/issues/detail?id=225. I am using the code there as reference but still is not working for me and then my application is crashing when it get to the hitTest.

Any suggestion? Thanks.

public boolean onTouchEvent(MotionEvent event, MapView mapView) {

    final int action = event.getAction();
    final int x = (int) event.getX();
    final int y = (int) event.getY();

    final Projection pj = mapView.getProjection();

    boolean result = false;
    //Object TAG;
    //Log.d(TAG, "onTouchEvent entered");

    System.out.println("onTouchEvent!");        
    Point p = new Point(0,0);
    Point t = new Point(0,0);

    //System.out.print(MotionEvent.ACTION_DOWN);
    //System.out.print(action);

    if (action == MotionEvent.ACTION_DOWN) {

        System.out.println("Action Down!");

        for (OverlayItem item : mOverlays) {

            // Create a new GeoPoint from pixel coordinates (x, y, pointReuse):
            //pj.fromMapPixels(x, y, t);

            // Convert the given GeoPoint to onscreen pixel coordinates (GeoPoint, pointOut):
            pj.toPixels(item.getPoint(), p);

            //System.out.println(t.x);
            //System.out.println(p.x);

            //System.out.println(t.y);
            //System.out.println(p.y);

            defaultMarker = item.getDrawable();

            if (hitTest(item, defaultMarker, x - p.x, y - p.y)) {
                System.out.println("Action Down -> IF!");
                result = true;
                inDrag = item;
                mOverlays.remove(inDrag);
                populate();

                xDragTouchOffset = 0;
                yDragTouchOffset = 0;

                setDragImagePosition(x, y);
                dragImage.setVisibility(View.VISIBLE);

                xDragTouchOffset = t.x - p.x;
                yDragTouchOffset = t.y - p.y;

                break;
            }
        }

    } 

    else if (action == MotionEvent.ACTION_MOVE && inDrag != null) {
        //dragImage.setVisibility(View.VISIBLE);
        setDragImagePosition(x, y);
        System.out.println("Action Move!");
        result = true;
    } 

    else if (action == MotionEvent.ACTION_UP && inDrag != null) {
        dragImage.setVisibility(View.GONE);

        GeoPoint pt = (GeoPoint) pj.fromPixels(x - xDragTouchOffset, y - yDragTouchOffset);
        OverlayItem toDrop = new OverlayItem(inDrag.getTitle(),
                inDrag.getSnippet(), pt);

        mOverlays.add(toDrop);
        populate();

        inDrag = null;
        result = true; 

        pj.fromMapPixels(x, y, t);

        if((t.x - p.x) == xDragTouchOffset && (t.y - p.y) == yDragTouchOffset) {
            System.out.println ("Do something here if desired because we didn't move item " + toDrop.getTitle() );
        }

        System.out.println("Action Up!");

    }
    System.out.print(inDrag);
    return (result || super.onTouchEvent(event, mapView));
}

private void setDragImagePosition(int x, int y) {
    RelativeLayout.LayoutParams lp=
            (RelativeLayout.LayoutParams)dragImage.getLayoutParams();
    lp.setMargins(x-xDragImageOffset-xDragTouchOffset,
            y-yDragImageOffset-yDragTouchOffset, 0, 0);
    dragImage.setLayoutParams(lp);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2024-12-20 06:27:35

我让上面的代码工作了。我只注意到我的工作代码和你的工作代码之间有两点不同。一是您发送的 hitTest 参数,尝试将

if (hitTest(item, defaultMarker, x - px, y - py)) {

更改为

if (hitTest(item, defaultMarker, tx - px, ty - py)) {

第二个是你应该取消注释 pj.fromMapPixels(x, y, t);

你也应该如果您想在拖动图像时看到图像,请取消注释 dragImage.setVisibility(View.VISIBLE);

I got the above code working. I only noticed two things different between my working code and yours. One is the hitTest paramaters you are sending, try changing

if (hitTest(item, defaultMarker, x - p.x, y - p.y)) {

to

if (hitTest(item, defaultMarker, t.x - p.x, t.y - p.y)) {

The second is that you should uncomment pj.fromMapPixels(x, y, t);

also you should probably uncomment dragImage.setVisibility(View.VISIBLE); if you want to see the image as you are dragging it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文