gpt4 book ai didi

android - 如何检测向左、向右、向上和向下的运动

转载 作者:行者123 更新时间:2023-12-05 00:08:31 26 4
gpt4 key购买 nike

我想知道是否可以使用下面的 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;
}

最佳答案

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);
}
}

有关详细信息,请参阅 this link ,您也可以使用(我会推荐)GestureDetectorOnFling() 作为访问的示例 this link ...

关于android - 如何检测向左、向右、向上和向下的运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16662721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com