Android开发教程: 触摸屏模拟实现方向键


改写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, 而不是说手指往上划拉或往下划拉了一下。

  1. /**  
  2.  * Re write onKeyDown() for SAMSUNG  
  3.  */  
  4. public boolean onTouchEvent(MotionEvent event) {   
  5.     // Set the game status to running   
  6.     if (event.getAction() == MotionEvent.ACTION_DOWN) {   
  7.         if (mMode == READY | mMode == LOSE) {   
  8.             initNewGame();   
  9.             setMode(RUNNING);   
  10.             update();   
  11.             return true;   
  12.         }   
  13.   
  14.         if (mMode == PAUSE) {   
  15.             setMode(RUNNING);   
  16.             update();   
  17.             return true;   
  18.         }   
  19.     }   
  20.   
  21.     float x = event.getX();   
  22.     float y = event.getY();   
  23.   
  24.     switch (event.getAction()) {   
  25.     case MotionEvent.ACTION_DOWN:   
  26.         mX = x;   
  27.         mY = y;   
  28.         update();   
  29.         return true;   
  30.     case MotionEvent.ACTION_UP:   
  31.         float dx = x - mX;   
  32.         float dy = y - mY;   
  33.         if (Math.abs(dx) >= TOUCH_TOLERANCE   
  34.                 || Math.abs(dy) >= TOUCH_TOLERANCE) {   
  35.   
  36.             if (Math.abs(dx) >= Math.abs(dy)) { // move from left -> right   
  37.                                                     // or right -> left   
  38.                 if (dx > 0.0f) {   
  39.                     turnTo(EAST);   
  40.                 } else {   
  41.                     turnTo(WEST);   
  42.                 }   
  43.   
  44.             } else { // move from top -> bottom or bottom -> top   
  45.                 if (dy > 0.0f) {   
  46.                     turnTo(SOUTH);   
  47.                 } else {   
  48.                     turnTo(NORTH);   
  49.                 }   
  50.             }   
  51.             update();   
  52.             return true;   
  53.         }   
  54.     }   
  55.   
  56.     return super.onTouchEvent(event);   
  57. }   
  58.   
  59. private void turnTo(int direction) {   
  60.     if (direction == WEST & mDirection != EAST) {   
  61.         mNextDirection = WEST;   
  62.     }   
  63.   
  64.     if (direction == EAST & mDirection != WEST) {   
  65.         mNextDirection = EAST;   
  66.     }   
  67.   
  68.     if (direction == SOUTH & mDirection != NORTH) {   
  69.         mNextDirection = SOUTH;   
  70.     }   
  71.   
  72.     if (direction == NORTH & mDirection != SOUTH) {   
  73.         mNextDirection = NORTH;   
  74.     }   
  75. }  

相关内容