I noticed that in Android API Level 11 (ICS) MotionEvent has a transform method that takes a Matrix. This would actually make something I’m doing somewhat cleaner, but I’m coding for Level 8.
Is there a convenient alternative to MotionEvent.transform() that works on earlier versions of Android? I’d like historical and multi-touch coordinates to all be transformed, and I’d like it to correctly handle the distinction between relative versus absolute coordinates, which is where things get kind of tricky.
What I really want to know is if can I create a MotionEvent that’s a transformed copy of another MotionEvent. I actually don’t need arbitrary transformations, but I need to be able to both translate and scale. I believe I can translate with offsetLocation, but I don’t see any way to scale a MotionEvent (prior to Level 11). In particular, how can I transform the coordinates of a multi-touch event? The obtain methods just take a single (x,y) pair, not an array.
The reason I want to do this is that I have one object that dispatches events to a set of “child” objects. Right now each of those children transforms the coordinates in a kind of ad hoc way. The parent (the dispatcher) knows what transformation needs to be performed, however, so it would be a lot cleaner to just transform the MotionEvent (or a copy) before dispatching it to the children. Alternatively I could create my own class that either wraps the MotionEvent or copies and transforms the data out of a MotionEvent, but it would be a lot simpler if I could use a MotionEvent.
As best as I can tell, the answer is “no”. There does not appear to be any way to manipulate the coordinates of any pointerId other than the first one in a
MotionEventin pre-Honeycomb versions of Android.The workaround I came up with was to create my own wrapper class (which I happened to call
TouchEvent) and use this instead ofMotionEventwhen delegatingonTouch. This wouldn’t work if I was implementing some standard interface, but in my case I controlled the interface of my delegates, so this solution works well enough.