15 January 2010

Adding Multitouch support

Since Android now supports multitouch, I wanted to find a code snippet illustrating its usage. Sounds like a piece of cake but actually isn't.
Once you get the job done, implementing multitouch on Android is really simple. See the snippet which will write out the x- and y-coordinates for each finger on the screen (this made me realize the Motorola Milestone can only identify 2 fingers at once) to LogCat:

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    for (int i=0; i<event.getPointerCount(); i++) {
    (...)
        Log.d("Pointer", "Pointer "+(i+1)+": x="+event.getX(i)+", y="+event.getY(i));
    } 
    (...)
} 
I found a topic about this on the Android Google group: http://groups.google.com/group/android-developers/browse_thread/thread/232ed8c380da7b64.
The provided (and simplified) snippet is based on this example app.

No comments:

Post a Comment