android ontouchevent,android-无法使用Osmdroid实现onTouchEvent(拖放)

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

我在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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值