How to simulate a touch event with Android while giving the X and Y coordinates manually?
The Chrome Browser integrates a nice feature to simulate multiple mobile devices or a touch screen. Now close the settings and press the ESC (Escape Key) and switch to the "Emulation" Panel (2). The "Sensors" menu (3) let's you enable the "Emulate touch screen" (4) checkbox. That's it!
Setup a touch listener In order to make your OpenGL ES application respond to touch events, you must implement the onTouchEvent() method in your GLSurfaceView class. The example implementation below shows how to listen for MotionEvent. ACTION_MOVE events and translate them to an angle of rotation for a shape.
Touch event works just like the dispatching of the events but in the reverse order from child to parent. Let's say if we dispatch the event from ViewGroup and intercept the event there, it depends on the return value (true/false) that shall the touch of the view be handled on the ViewGroup or the children.
Valentin Rocher's method works if you've extended your view, but if you're using an event listener, use this:
view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { Toast toast = Toast.makeText( getApplicationContext(), "View touched", Toast.LENGTH_LONG ); toast.show(); return true; } }); // Obtain MotionEvent object long downTime = SystemClock.uptimeMillis(); long eventTime = SystemClock.uptimeMillis() + 100; float x = 0.0f; float y = 0.0f; // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState() int metaState = 0; MotionEvent motionEvent = MotionEvent.obtain( downTime, eventTime, MotionEvent.ACTION_UP, x, y, metaState ); // Dispatch touch event to view view.dispatchTouchEvent(motionEvent);
For more on obtaining a MotionEvent object, here is an excellent answer: Android: How to create a MotionEvent?
Here is a monkeyrunner script that sends touch and drags to an application. I have been using this to test that my application can handle rapid repetitive swipe gestures.
# This is a monkeyrunner jython script that opens a connection to an Android # device and continually sends a stream of swipe and touch gestures. # # See http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html # # usage: monkeyrunner swipe_monkey.py # # Imports the monkeyrunner modules used by this program from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice # Connects to the current device device = MonkeyRunner.waitForConnection() # A swipe left from (x1, y) to (x2, y) in 2 steps y = 400 x1 = 100 x2 = 300 start = (x1, y) end = (x2, y) duration = 0.2 steps = 2 pause = 0.2 for i in range(1, 250): # Every so often inject a touch to spice things up! if i % 9 == 0: device.touch(x2, y, 'DOWN_AND_UP') MonkeyRunner.sleep(pause) # Swipe right device.drag(start, end, duration, steps) MonkeyRunner.sleep(pause) # Swipe left device.drag(end, start, duration, steps) MonkeyRunner.sleep(pause)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With