3

我想知道是否可以使用下面的 onTouch 方法来检测用户是否在屏幕上向左、向右、向上、向下移动手指。我有一个带有对象的网格,我只希望用户能够在四个方向上移动这个对象之一。

我的想法是使用 Action_Down 事件来获取 X 和 Y 位置,然后检查列表中的所有对象以查看哪个对象与 X 和 Y 值一致。然后通过使用 Action_Move 开始向其中一个方向移动它。

    @Override
public boolean onTouch(View v, MotionEvent event) {

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
        Log.i("test","Down");
        break;

    case MotionEvent.ACTION_POINTER_UP:

        break;

    case MotionEvent.ACTION_MOVE:
        Log.i("test","Move");
        break;
    }
4

1 回答 1

-1
public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
@Override
public boolean onTouchEvent(MotionEvent event){ 

    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
        case (MotionEvent.ACTION_DOWN) :
            Log.d(DEBUG_TAG,"Action was DOWN");
            return true;
        case (MotionEvent.ACTION_MOVE) :
            Log.d(DEBUG_TAG,"Action was MOVE");
            return true;
        case (MotionEvent.ACTION_UP) :
            Log.d(DEBUG_TAG,"Action was UP");
            return true;
        case (MotionEvent.ACTION_CANCEL) :
            Log.d(DEBUG_TAG,"Action was CANCEL");
            return true;
        case (MotionEvent.ACTION_OUTSIDE) :
            Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
                    "of current screen element");
            return true;      
        default : 
            return super.onTouchEvent(event);
    }      
}

有关更多详细信息,请参阅此链接,您还可以使用(我会推荐) GestureDetector的OnFling ()作为访问此链接的示例...

于 2013-05-21T05:30:51.753 回答