Gesture detection in NDK

543 views
Skip to first unread message

Andreas Falkenhahn

unread,
Mar 1, 2013, 1:37:27 PM3/1/13
to andro...@googlegroups.com
Hi,

as a NDK programmer, how am I supposed to detect gestures? I tried to use GestureDetector
class over on the Java side of my app but it doesn't seem to work. Specifically, the
onTouch() event handler of my NativeActivity (that calls onTouchEvent() of the gesture
detector) never gets called, i.e. the following code inside the onCreate() of the
NativeActivity:

mGestureListener = new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
Log.i("DEBUG", "*** THIS NEVER HAPPENS ***");
return mGestureDetector.onTouchEvent(event);
}
};

Is there a possibility to use the GestureDetector from the NDK? Should I simply do a
JNI call to mGestureDetector.onTouchEvent() whenever I get a AINPUT_EVENT_TYPE_MOTION
event from AInputEvent_getType()? Would that work?

Tks

Andreas

Andreas Falkenhahn

unread,
Mar 3, 2013, 10:04:25 AM3/3/13
to andro...@googlegroups.com
As it doesn't seem to be possible to intercept MotionEvents on the Java side before
they are passed to the C side, I've now tried it the other way round: Whenever I
get an AINPUT_EVENT_TYPE_MOTION on the C side, I'm feeding this to the GestureDetector
on the Java side through JNI like this:

(*env)->CallVoidMethod(env, g_android->activity->clazz, (*env)->GetMethodID(env, globalMyNativeActivityClass, "runGestureDetector", "(JJIIFFFFIFFII)V"), AMotionEvent_getDownTime(event), AMotionEvent_getEventTime(event), AMotionEvent_getAction(event), AMotionEvent_getPointerCount(event), AMotionEvent_getRawX(event, 0), AMotionEvent_getRawY(event, 0), AMotionEvent_getPressure(event, 0), AMotionEvent_getSize(event, 0), AMotionEvent_getMetaState(event), AMotionEvent_getXPrecision(event), AMotionEvent_getYPrecision(event), AInputEvent_getDeviceId(event), AMotionEvent_getEdgeFlags(event));

The Java method runGestureDetector() simply does this then:

public void runGestureDetector(long downTime, long eventTime, int action, int pointerCount, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags) {

mGestureDetector.onTouchEvent(MotionEvent.obtain(downTime, eventTime, action, pointerCount, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags));
}

By doing it this way I'm able to detect some gestures. onDown(), onSingleTap() and onScroll()
come in just fine. What is not working, however, is onFling(). But onFling() is the most
important one for me.

My suspection is that onFling() does not work because it might rely on the historical values
that can be stored in a MotionEvent (every MotionEvent has a historical size with historical
parameters, see android/input.h). However, the obtain() constructor of MotionEvent class does
not allow me to construct a MotionEvent object with historical values.

So is there any way to feed the real MotionEvent that I get on the C side to onTouchEvent() on
the Java side? I can get the historical values through the NDK API but AFAICS it is not possible
to construct a MotionEvent with historical information on the Java side :-(

Any ideas?

Tks,

Andreas
--
Best regards,
Andreas Falkenhahn mailto:and...@falkenhahn.com

Reply all
Reply to author
Forward
0 new messages