Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
长时间触摸surfaceView(android)_Android_Multithreading_Touch_Long Press - Fatal编程技术网

长时间触摸surfaceView(android)

长时间触摸surfaceView(android),android,multithreading,touch,long-press,Android,Multithreading,Touch,Long Press,我正在Android上制作一个游戏,当用户试图在屏幕上长按时,我需要做一些动作。不幸的是,我还没有找到任何可以直接使用自定义SurfaceView的方法,请随时告诉我是否存在这样的方法:) 因此,我决定尝试从onTouch事件侦听器实现一个长触摸检测 这是我的密码: @Override public boolean onTouch(View v, MotionEvent event) { long touchDuration = 0;

我正在Android上制作一个游戏,当用户试图在屏幕上长按时,我需要做一些动作。不幸的是,我还没有找到任何可以直接使用自定义SurfaceView的方法,请随时告诉我是否存在这样的方法:)

因此,我决定尝试从onTouch事件侦听器实现一个长触摸检测

这是我的密码:

@Override
    public boolean onTouch(View v, MotionEvent event)
    {
        long touchDuration = 0;


            if ( event.getAction() == MotionEvent.ACTION_DOWN )
            {
                //Start timer
                touchTime = System.currentTimeMillis();


            }else if ( event.getAction() == MotionEvent.ACTION_UP )
            {
                //stop timer
                touchDuration = System.currentTimeMillis() - touchTime;

                if ( touchDuration < 800 )
                {
                    onShortTouch(event,touchDuration);
                }else
                {
                    onLongTouch(event,touchDuration);
                }
            }
        }

        return true;
有人知道吗?另一种解决办法也将受到欢迎


谢谢。

当您还想进行一些手动事件处理时,GestureDetector类是一个不错的选择:

我如何使用它在构造函数中创建一个GestureDetector实例,实现我感兴趣的方法,然后在onTouchEvent()方法开始时执行以下操作:

    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }

这就意味着在大多数情况下,当检测到一个手势时,会中止其余的触摸操作。但是,longpress有点不同,因为longpress方法不返回值。在这种情况下,我只是在触发长按时设置了一个布尔值,如有必要,我会在稍后的触摸事件处理方法中进行检查。

您可以做的一件事是在SurfaceView上设置OnLongClickListener

mySurfaceView.setOnLongClickListener(l);
然后在SurfaceView中,您需要覆盖onTouchEvent:

public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);
    return true;
}

事实上,这很容易。在视图的构造函数中,执行以下操作:

setOnLongClickListener(new View.OnLongClickListener() 
{
   public boolean onLongClick(View v) {
        Log.v( "debug", "LONG CLICK!" );
        return true;
     }
  }
);
然后覆盖onTouchEvent:

  @Override
  public boolean onTouchEvent(MotionEvent event) {
      super.onTouchEvent(event);
  }

你准备好了;)

感谢
super.onTouchEvent(事件)是我的关键。
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      super.onTouchEvent(event);
  }