1

I'm currently implementing a gesture detector for multi-touch events on Android. For testing the behaviour of the detector, i want to send MotionEvents into the detector and check his actions.

The problem i am currently having is that i can only create MotionEvents by calling one of the existing MotionEvent.obtain() methods, but it seems these methods do not allow me to set the pointer id for an event. I.e. i can only create single-touch events.

Does anyone know if it's possible to do this somehow? I could use another datastructure for input to the detector, but if possible i want to avoid this and stick with the MotionEvent class.

Maik
  • 3,419
  • 1
  • 23
  • 34

3 Answers3

1

Have you tried:

public static MotionEvent obtain (long downTime, long eventTime, int action, 
int pointers, int[] pointerIds, PointerCoords[] pointerCoords, int metaState, 
float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags)

which is the third obtain( ) method listed in MotionEvent page

You can specify:

  • pointers - The number of points that will be in this event.
  • pointerIds - An array of pointers values providing an identifier for each pointer.
  • pointerCoords - An array of pointers values providing a MotionEvent.PointerCoords coordinate object for each pointer.
f20k
  • 3,106
  • 3
  • 23
  • 32
  • f20k, Are you able to give any simple example for multitouch using the method you have pointed. I'm trying to do a multitouch from my testing class, but I can't any sample how to use this method with 2 pointers... Thanks in advance. – user643154 Mar 05 '11 at 17:12
1

I've ended up setting my test project API level to 9 while the main project is still API 7, so I can use the new MotionEvent.obtain with PointerCoords[] on my tests. It's working fine.

Jonas Alves
  • 1,306
  • 12
  • 17
0

With Android SDK 2.3 there is a new method, as already mentioned by f20k, which fixes the problem.

But i'm currently stuck with api level 7 (2.1). My solution was to add a layer between my touch handling code and the actual events i get from Android, simply by converting to my own MotionEvent class (i named it TouchInfo). Furthermore, this allowed me to implement the whole gesture handling on the JVM which allowed me to drive the code by TDD. I could have done it with Android JUnit tests too, but developing the code on the JVM allowed me to use things like junit4, hamcrest and mockito.

A third way would be to use robolectric if you don't want to add the layer.

Maik
  • 3,419
  • 1
  • 23
  • 34