Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Android handle transformation of touch points in child views?

When a child view is rotated, how does Android handle mapping touch points from the parent view to the child view?

Are the touch points put through a transformation at some point? If so, where?

I know that ViewGroups have methods like: dispatchTouchEvent() onInterceptTouchEvent() onTouchEvent() etc.

Is one of those responsible for taking the touch point within the parent view and transforming it to the local coordinate space of a subview?

I tried looking through the source code but couldn't really understand all the interactions.

like image 859
Studio4Development Avatar asked Mar 24 '23 00:03

Studio4Development


1 Answers

All this stuff is performed in dispatchTouchEvent method.

  1. ViewGroup iterates through it children
  2. Creating new copy of passed to dispatchTouchEvent MotionEvent for each child (obtaining from pool to be more precise). See MotionEvent.obtain(MotionEvent)
  3. Offsets position of newly created MotionEvent using child's top and left position (see getTop and getLeft methods).
  4. Applies transformation using MotionEvent#transform method (by taking inverse transformation matrix from child)
  5. Finally, it dispatches MotionEvent to child (and in case when this child is also ViewGroup see pt.1)
  6. It recycles previously obtained MotionEvent (see MotionEvent#recycle)

And yes, as Delyan mentioned - transformation applies only for Honeycomb and newer versions of Android. On older versions just offset is performed.

like image 154
Dmitry Zaytsev Avatar answered Apr 26 '23 11:04

Dmitry Zaytsev