问题
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