开发者

why does return value of onTouchEvent either crashes application or freezes mapView?

开发者 https://www.devze.com 2023-04-11 19:03 出处:网络
I am working on a google map application where user can touch the map and mark start and end points over the map. Then the path connecting the two points is draw on the map.

I am working on a google map application where user can touch the map and mark start and end points over the map. Then the path connecting the two points is draw on the map. To handle touch events I have implemented onTouchEvent method inside MapOverlay class. I have observed some weird behavior with the map. When user gets a path connecting two points the map freezes i.e. i can't pan or zoom over the map. I found out that if the return value is true inside the onTouchEvent which is inside the MapOverlay method the map freezes in above said circumstance. If i return false then map won't freeze. However if I want to mark new points on the map again the application crashes... How is return value of onTouchEvent r开发者_如何学Goelated to this behavior?


The way onTouchEvent() even works is if you return true, it means the touch event was handled and Android won't continue down the view hierarchy to handle the touch event. If you return false, it will continue.

In this case, when you always return true, you're always saying the touch event was handled. Thus, any gestures you do will never be handled because you're saying it's already handled. Thus, it looks like the map freezes. Instead, put a condition like this:

@Override
public boolean onTouchEvent(MotionEvent event){
   boolean wasHandled = false;
   if(/* condition where your touch event was handled */)
      wasHandled = true;
   else
      wasHandled = super.onTouchEvent(event);

   return wasHandled;
}

The reason it crashes when you return false is either a problem in your program or it goes into a loop because the event's never handled.


If it returns true, the handler tells the system, that it handled the onTouchEvent and the system wont call another onTouchListener. If you return false, you tell the system, that you did not handle the touch event and it will call the onTouchListener of the underlying view. You map does not freeze, but it does not get any touch events, because your handler handles them.

0

精彩评论

暂无评论...
验证码 换一张
取 消