java - MotionEvent multitouch offset (multiple pointers) -


i have implemented zoomviewgroup, capable of scrolling infinitely in directions , zooming infinitely while still delivering touch events correctly offset child views.

but when comes multi-touch, first pointer offset correctly, , others pointing wrong location, , thats because there scaling factor have take care of. (as pointer 0 has different offset original location pointer 1 or 2, when scaling_factor != 1.0f)

i saving transformation in matrix, it's easy calculate coordinates screen workspace , using matrix.mappoints(..) , matrix inverse.

when draw child views, can apply transformation matrix this:

protected void dispatchdraw(canvas canvas) {     canvas.save();     canvas.concat(transformation_matrix);      super.dispatchdraw(canvas);     canvas.restore(); } 

same touch events:

float[] touch_array = new float[2];  public boolean dispatchtouchevent(motionevent event) {     touch_array[0] = event.getx();     touch_array[1] = event.gety();     transformation_matrix.mappoints(touch_array);      event.setlocation(touch_array[0], touch_array[1]);     return super.dispatchtouchevent(event); } 

but motionevent.setlocation(float x, float y) offset pointers same amount. if zoomed 2.0f offset different each pointer, have able motionevent.setloction(int pointer_index, float x, float y) each 1 individually. there can achieve this?

edit: solution know (and searched for) far create new motionevent new coordinates.

--

you can pointers (fingers touching) positions trough methods getx(int pointerid)/gety(int).

you can on screen pointers trough getpointercount()

so parse multi-touch must like:

public boolean dispatchtouchevent(motionevent event) {     for(int = 0; < event.getpointercount(); i++){         touch_array[0] = event.getx(i);         touch_array[1] = event.gety(i);         transformation_matrix.mappoints(touch_array);         motionevent copy = motionevent.obtainnohistory(event);         copy.setlocation(touch_array[0], touch_array[1]);         boolean handled = super.dispatchtouchevent(copy);         copy.recycle();         if(handled) return true;     }     return false; } 

also note created copy of motionevent object, childrem can modify without breaking position trough for.

sorry, noticed last statement, must copy event new motionevent, childrem can parse , works, copying make code safe of modifications, it's bad idea change motionevent directly since can break loop in dispatch method.


Comments