Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid multitouch! hack anyone?
    primarykey
    data
    text
    <p>I have to let this slip for now as a purely academic issue but i would very much like to see a solution in near time.</p> <p>Due to the way that Android handles multitouch you can (as i see it) only trap the event in a single view. I've tried an hack for this envolving a container layout that intercepts the events sees what View it belongs by seeing the coords and changing the action itself so that it seems to the component that it's a single touch event. I compose such events and then route it to the Views.</p> <p>Does anyone have a better idea to do this?</p> <p>If someone wants the code for what i described above just ask and i post it!</p> <p>Have fun and good luck :D JQCorreia</p> <pre><code>public class Container extends LinearLayout { LinkedHashMap&lt;Integer,View&gt; pointers = new LinkedHashMap&lt;Integer,View&gt;(); ArrayList&lt;View&gt; views = new ArrayList&lt;View&gt;(); public Container(Context context) { super(context); initialize(context); } public Container(Context context, AttributeSet attrs) { super(context, attrs); initialize(context); } private void initialize(Context context) { } @Override public void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); views = LayoutUtil.flattenLayout(this,false); for(View foo : views) { Rect rect = new Rect(); foo.getGlobalVisibleRect(rect); } } @Override public boolean onInterceptTouchEvent(MotionEvent event) { return true; } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction() &amp; MotionEvent.ACTION_MASK; if(action==MotionEvent.ACTION_DOWN) { for(View v: views) { Rect r = new Rect(); v.getGlobalVisibleRect(r); if (event.getX() &gt; r.left &amp;&amp; event.getX() &lt; r.right &amp;&amp; event.getY() &gt; r.top &amp;&amp; event.getY() &lt; r.bottom) { pointers.put(event.getPointerId(0),v); pointers.get(event.getPointerId(0)).onTouchEvent(event); break; } } } if(action==MotionEvent.ACTION_POINTER_DOWN) { int pid = event.getAction() &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT; int index = event.findPointerIndex(pid); for(View v: views) { Rect r = new Rect(); v.getGlobalVisibleRect(r); if (event.getX(index) &gt; r.left &amp;&amp; event.getX(index) &lt; r.right &amp;&amp; event.getY(index) &gt; r.top &amp;&amp; event.getY(index) &lt; r.bottom) { pointers.put(pid,v); MotionEvent copy = MotionEvent.obtain(event); copy.setAction(MotionEvent.ACTION_DOWN); copy.setLocation(event.getX(index), event.getY(index)); pointers.get(pid).onTouchEvent(copy); } } } if(action==MotionEvent.ACTION_POINTER_UP) { int pid = event.getAction() &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT; int index = event.findPointerIndex(pid); if(pointers.get(pid)!=null) // If the touch was outside any view { MotionEvent copy = MotionEvent.obtain(event); copy.setAction(MotionEvent.ACTION_UP); pointers.get(pid).onTouchEvent(copy); pointers.remove(pid); } } if(action==MotionEvent.ACTION_MOVE) { for(int i = 0; i&lt;event.getPointerCount();i++) { int pid = event.getPointerId(i); MotionEvent copy = MotionEvent.obtain(event); copy.setLocation(event.getX(i), event.getY(i)); if(pointers.get(pid)==null) continue; // If the touch was outside any view pointers.get(pid).onTouchEvent(copy); } } if(action==MotionEvent.ACTION_UP) { if(pointers.get(event.getPointerId(0))!=null) { pointers.get(event.getPointerId(0)).onTouchEvent(event); pointers.remove(event.getPointerId(0)); } } return true; } } // This is the LayoutUtil.flattenLayout method public static ArrayList&lt;View&gt; flattenLayout(View view, boolean addViewGroups) { ArrayList&lt;View&gt; viewList = new ArrayList&lt;View&gt;(); if(view instanceof ViewGroup) { if(((ViewGroup)view).getChildCount()==0) viewList.add(view); else { if(addViewGroups) { viewList.add(view); } ViewGroup viewgroup = (ViewGroup) view; for(int i = 0; i &lt; viewgroup.getChildCount();i++) { viewList.addAll(flattenLayout(viewgroup.getChildAt(i),false)); } } } else if(view instanceof View) { viewList.add(view); } return viewList; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload