gpt4 book ai didi

SurfaceView 中带有 SimpleOnGestureListener 的 Android GestureDetector

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:24 25 4
gpt4 key购买 nike

public class GameActivity extends Activity {
private static final String TAG = "GameActivity";

. . .

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}

. . .
}


public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "GameView";

. . .

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
gestureDetector.onTouchEvent(event);
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}

private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
private static final String TAG = "GestureListener";

@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(TAG, "+ onSingleTapConfirmed(event:" + event + ")");
singleTapDetected = true;
Log.d(TAG, "- onSingleTapConfirmed()");
return true;
}

@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(TAG, "+ onDoubleTap(event:" + event + ")");
doubleTapDetected = true;
Log.d(TAG, "- onDoubleTap()");
return true;
}
};

private GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);

. . .
}

我在 AVD 上运行它并在 SurfaceView (GameView) 上单击一下。
根据 LogCat 日志:

03-11 14:19:51.171: D/GameView(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameView(4839): - onTouchEvent()
03-11 14:19:51.171: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameActivity(4839): - onTouchEvent()
03-11 14:19:51.299: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223303, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.299: D/GameActivity(4839): - onTouchEvent()

有几个问题:

  1. 为什么没有调用 SimpleOnGestureListener? (尽管 onTouchEvent() 被调用并且大概行 gestureDetector.onTouchEvent(event); 被执行)

  2. 为什么 GameActivity 的 onTouchEvent() 获得 ACTION_DOWNACTION_UP,而 GameView 的 onTouchEvent() 只得到ACTION_DOWN

我尝试过的一些东西

  • 我在 GameActivity 类上实现了 GestureDetector,代码如上所示,它按预期工作,调用 onSingleTapConfirmed()

  • 在 GameView 中,创建了一个私有(private)的 class GestureListener extends SimpleOnGestureListener,而不是私有(private)成员 SimpleOnGestureListener,并用它来构造 GestureDetector。我看到了与描述相同的行为,没有调用监听器。

最佳答案

我把代码调试到android源码中,GestureDetector类。这解释了为什么 SimpleOnGestureListener没有被调用:

  • 收到 ACTION_DOWN 并向 GestureDetector 消息处理程序生成内部 TAP 消息。
  • Handler 调用监听器 onSingleTapConfirmed()仅当事件仍然下降时 ( !mStillDown )
  • 事件仍然失败,因为从未收到 ACTION_UP 事件。

所以你看,这两个问题是相关的,当我找到为什么没有发出 ACTION_UP 时,我会解决问题!

编辑

这就完成了答案,现在可以正常工作了。

  • 调用super.onTouchEvent()在 GameView 中返回 false ,因为 super SurfaceView的类是View,返回false .这就是为什么从不调用 ACTION_UP 的原因
  • 调用super.onTouchEvent()在 GameActivity 中返回 true ,这就是在 GameActivity 上发出 ACTION_UP 的原因。

注意:如果我设置 GameView.setclickable(true) 然后 super.onTouchEvent() 返回 true . 这也是一个可以接受的解决方案

现在我的 GameView 代码是:

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
gestureDetector.onTouchEvent(event);
Log.d(TAG, "- onTouchEvent()");
return true;
}

当前的 LogCat 日志是:

03-11 16:32:06.629: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.629: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.641: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.641: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.772: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158772, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.772: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.931: D/GestureListener(5316): + onSingleTapConfirmed(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.931: D/GestureListener(5316): - onSingleTapConfirmed()

关于SurfaceView 中带有 SimpleOnGestureListener 的 Android GestureDetector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341736/

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