Android穿戴 - 如何捕捉触摸事件

时间:2014-06-28 07:55:37

标签: touch wear-os

我想捕捉Android上的触摸事件(我正在使用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;
                }
            });
        }
    });
}

2 个答案:

答案 0 :(得分:3)

您可以尝试仅使用:

container.setOnTouchListener(new View.OnTouchListener() {
从那里

return true?您需要通知您消耗此事件的View

答案 1 :(得分:-1)

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

adb shell getevent