改写Android的Snake例子,使之运行于我的三星手机上。判断规则如下:如果x方向移动距离大于y方向,则认为是水平移动,反之则是上下移动。如果水平移动,x移动正距离x-x0>0 则认为向右移动,负距离x-x0<0 则认为向左移动;上下移动的判断同理。
代码如下,需要注意的是MotionEvent的ACTION_DOWN, ACTION_UP 是这么理解的:
ACTION_DOWN - A pressed gesture has started, the motion contains the initial starting location.
ACTION_UP - A pressed gesture has finished, the motion contains the final release location as well as any intermediate points since the last down or move event.
ACTION_MOVE - A change has happened during a press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}). The motion contains the most recent point, as well as any intermediate points since the last down or move event. -
简而言之,ACTION_DOWN, ACTION_UP 类似于Javascript里面键盘事件OnKeyDown, OnKeyUp 或鼠标事件OnMouseDown, OnMouseUp, 而不是说手指往上划拉或往下划拉了一下。
/**
* Re write onKeyDown() for SAMSUNG