1

我想在 Android Wear(我使用的是 Samsung Gear Live)上捕捉触摸事件来绘制触摸轨迹。我尝试将 onTouch 事件和 onGenericMotionEvent 事件捕获为以下代码,但该事件仅触发一次,或者有时在我在屏幕上滑动时不会发生。我需要更多的触摸事件(从左向右滑动时至少需要 5 到 6 个事件)来绘制轨迹。如何在 Android Wear 屏幕上捕获足够的触摸事件?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            ViewGroup container = (ViewGroup) findViewById(R.id.container);
            container.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    String s = "";
                    s += "action=" + event.getAction();
                    s += ", X=" + event.getX();
                    s += ", Y=" + event.getY();
                    Log.d(TAG, s);
                    return false;
                }
            });
            container.setOnGenericMotionListener(new View.OnGenericMotionListener() {
                @Override
                public boolean onGenericMotion(View view, MotionEvent event) {
                    String s = "";
                    s += "action=" + event.getAction();
                    s += ", X=" + event.getX();
                    s += ", Y=" + event.getY();
                    Log.d(TAG, s);
                    return false;
                }
            });
        }
    });
}
4

2 回答 2

4

您可以尝试仅使用:

container.setOnTouchListener(new View.OnTouchListener() {

从那里return true开始?您需要通知View您消费了该事件。

于 2014-06-28T17:11:04.820 回答
-1

如果您想离线调试,您可以从 adb 获取触摸坐标:-

adb shell getevent
于 2017-03-23T23:57:55.143 回答