简体   繁体   English

如何检测向左,向右,向上和向下的移动

[英]How to detect movement to the left, right, up and down

I wonder if it's possible to use the onTouch method below to detect if the user is moving the finger to the left, right, up och down on the screen. 我想知道是否可以使用下面的onTouch方法检测用户是否在屏幕上向左,向右,向上或向下移动手指。 I have a grid with objects and I just want the user to be able to move one of this object in four directions. 我有一个带有对象的网格,我只希望用户能够在四个方向上移动该对象之一。

My idea was to use the Action_Down event to get the X and Y position and then check all objects in a list to see which object that is whitin the X and Y value. 我的想法是使用Action_Down事件获取X和Y位置,然后检查列表中的所有对象,以查看哪个对象具有X和Y值。 Then by using the Action_Move start moving it in one of the directions. 然后通过使用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;
    }
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);
    }      
}

For more details see this link , You can also use (and I will recommend that) OnFling() of GestureDetector for an example of that visit this link ... 欲了解更多详情,请参阅此链接 ,您也可以使用(我会建议)GestureDetectorOnFling()来访问的例子此链接 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM