Android wear - how to capture touch events

 ̄綄美尐妖づ 提交于 2019-12-02 09:59:06

问题


I want to capture the touch events on Android wear (I am using Samsung Gear Live) to draw the trajectory of touch. I tried to capture onTouch event and onGenericMotionEvent event as the following code, but the event is triggered only one time or sometimes does not happen while I swiping on the screen. I need more touch events (at least 5 to 6 events when I swipe left to right) to draw the trajectory. How I can capture enough touch events on Android Wear screen?

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

回答1:


Can you try using only:

container.setOnTouchListener(new View.OnTouchListener() {

and return true from there? You need to inform the View you consumed the event.




回答2:


If you want to debug offline you can get the touch co-ordinates from adb:-

adb shell getevent


来源:https://stackoverflow.com/questions/24464834/android-wear-how-to-capture-touch-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!