Android如何处理子视图中触摸点的转换?

Stu*_*ent 4 android android-custom-view touch-event android-layout

旋转子视图时,Android如何处理从父视图到子视图的触摸点映射?

接触点是否在某个时刻进行了转换?如果是的话,在哪里?

我知道ViewGroups有类似的方法: dispatchTouchEvent() onInterceptTouchEvent() onTouchEvent() 等.

是其中一个负责在父视图中获取触摸点并将其转换为子视图的本地坐标空间?

我尝试查看源代码,但无法真正理解所有的交互.

Dmi*_*sev 7

所有这些都是用dispatchTouchEvent方法完成的.

  1. ViewGroup 通过它迭代孩子们
  2. dispatchTouchEvent MotionEvent为每个子项创建传递给的新副本(从池中获取更精确).看到MotionEvent.obtain(MotionEvent)
  3. MotionEvent使用子项的上下左侧位置偏移新创建的位置(请参阅getTopgetLeft方法).
  4. 使用MotionEvent#transform方法应用变换(通过从子进行逆变换矩阵)
  5. 最后,它发送MotionEvent给孩子(如果这个孩子也ViewGroup看到pt.1)
  6. 它回收以前获得的MotionEvent(见MotionEvent#recycle)

是的,正如Delyan所提到的 - 转换仅适用于Honeycomb和更新版本的Android.在旧版本上,只执行偏移.