I'm somehow getting unexpected results while trying to implement multitouch in my app. I'm never getting data for more than one pointer.
Multitouch on my phone surely works, because I can pinch-zoom in browser and detect pinch gesture with GestureDetector, but the following sample prints action=0 pointers=1
regardless of how many fingers I use to touch the screen.
Is there something in configuration/AndroidManifest or Activity creation that I need to
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.ll1).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TAG","onTouch action="+event.getAction()+" pointers="+event.getPointerCount());
return false;
}
});
}
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
android.view.MotionEvent. Object used to report movement (mouse, pen, finger, trackball) events. Motion events may hold either absolute or relative movements and other data, depending on the type of device.
ACTION_DOWN is for the first finger that touches the screen. This starts the gesture. The pointer data for this finger is always at index 0 in the MotionEvent. ACTION_POINTER_DOWN is for extra fingers that enter the screen beyond the first.
After the Math. abs() calls, you're essentially testing if their finger is farther down the screen than it is across the screen. Store the initial down coordinates as member variables and set them during ACTION_DOWN . You declared two floats (touchX and touchY) inside the onTouchEvent method.
You should use one of the static obtain methods of the MotionEvent class to create a new event. API Docs: Create a new MotionEvent, filling in a subset of the basic motion values. Those not specified here are: device id (always 0), pressure and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
The problem was that I was returning false
in onTouch
, therefore new touch events have not been generated.
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