不忘初心 砥砺前行, Tomorrow Is Another Day !html
本文概要:java
横轴:方向从左至右,数值依次增大. 纵轴:方向从上至下,数值依次增大.bash
left、top、bottom、right与x,y,transLationX,transLationY之间的关系ide
其中x,y表明View左上角的坐标,transLationX,transLationY表明View左上角的坐标偏移量.须要明确的是这四个参数都是相对于父容器坐标是一种相对坐标.post
当在作平移的过程当中,left与top的原始信息不会发生改变,改变的是x,y,transLationX,transLationY的值.学习
对于触摸事件须要明白几点.动画
主要涉及到下面三个方法.ui
总体流程具体参考下面伪代码,我的以为将触摸事件流程体现的淋淋尽致.this
/*
* 经典伪代码
*/
public boolean dispatchTouchEvent(MotionEvent ev) {
boolean comsume;
//ViewGroup是否拦截事件
if (onInterceptTouchEvent(ev)) {//true拦截,本身处理
if(!OnTouchListener.onTouch()){//根据OnTouchListener的OnTouch决定是否调用.
comsume = onTouchEvent(ev);
}
} else {//不拦截,交给子View处理
comsume = child.dispatchTouchEvent(ev);
}
}
复制代码
对于上面伪代码咱们来对总体流程作个总结spa
具体处理流程
若是到这里,上面介绍的触摸事件内容都能很清晰的理解,那么暂时不用看下面原理分析内容,能够本身试着去分析View的事件分发机制的源码,会有不同的收获与感觉.
Activity的分发过程
事件由Activity调用dispatchTouchEvent首先传入到Window,而Window的惟一实现类是PhoneWindow,最终传递到顶级View(DecorView),一直往下传递.若是这些事件都没有处理,最终又会交给Activity的onTouchEvent进行处理.
对应源码
//Activity.java
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
onUserInteraction();
}
//调用window的superDispatchTouchEvent
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
//PhoneWindow.java
@Override
public boolean superDispatchTouchEvent(MotionEvent event) {
//调用DecorView的superDispatchTouchEvent
return mDecor.superDispatchTouchEvent(event);
}
//DecorView.java
public boolean superDispatchTouchEvent(MotionEvent event) {
//调用ViewGroup的dispatchTouchEvent
return super.dispatchTouchEvent(event);
}
复制代码
ViewGroup的分发过程
ViewGroup的dispatchTouchEvent分发过程比较复杂分层2个部分去看.
对应源码
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//...省略部分代码
boolean handled = false;
if (onFilterTouchEventForSecurity(ev)) {
final int action = ev.getAction();
final int actionMasked = action & MotionEvent.ACTION_MASK;
/*
* 1.当是ACTION_DOWN事件时,则重置状态
*/
if (actionMasked == MotionEvent.ACTION_DOWN) {
cancelAndClearTouchTargets(ev);
resetTouchState();
}
/*
2.检查是否拦截
mFirstTouchTarget: 记录消耗事件的子view
*/
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) {//DOWN事件或者有子View消耗了事件
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {//检查标记位
//回调onInterceptTouchEvent
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
intercepted = true;
}
复制代码
对应源码
if (newTouchTarget == null && childrenCount != 0) {
final float x = ev.getX(actionIndex);
final float y = ev.getY(actionIndex);
// Find a child that can receive the event.
// Scan children from front to back.
//点击位置多个子View重叠问题处理
final ArrayList<View> preorderedList = buildTouchDispatchChildList();
final boolean customOrder = preorderedList == null
&& isChildrenDrawingOrderEnabled();
final View[] children = mChildren;
/*
1. 遍历全部子View
*/
for (int i = childrenCount - 1; i >= 0; i--) {
final int childIndex = getAndVerifyPreorderedIndex(
childrenCount, i, customOrder);
final View child = getAndVerifyPreorderedView(
preorderedList, children, childIndex);
// If there is a view that has accessibility focus we want it
// to get the event first and if not handled we will perform a
// normal dispatch. We may do a double iteration but this is
// safer given the timeframe.
if (childWithAccessibilityFocus != null) {
if (childWithAccessibilityFocus != child) {
continue;
}
childWithAccessibilityFocus = null;
i = childrenCount - 1;
}
/*
2. 寻找是否能接受点击事件
canViewReceivePointerEvents:是否在播放动画
isTransformedTouchPointInView:点击事件的坐标是否落在子元素区域内
*/
if (!canViewReceivePointerEvents(child)
|| !isTransformedTouchPointInView(x, y, child, null)) {
ev.setTargetAccessibilityFocus(false);
continue;
}
newTouchTarget = getTouchTarget(child);
if (newTouchTarget != null) {
// Child is already receiving touch within its bounds.
// Give it the new pointer in addition to the ones it is handling.
newTouchTarget.pointerIdBits |= idBitsToAssign;
break;
}
resetCancelNextUpFlag(child);
/*
3.事件传递给子View处理,
*/
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {
// Child wants to receive touch within its bounds.
mLastTouchDownTime = ev.getDownTime();
if (preorderedList != null) {
// childIndex points into presorted list, find original index
for (int j = 0; j < childrenCount; j++) {
if (children[childIndex] == mChildren[j]) {
mLastTouchDownIndex = j;
break;
}
}
} else {
mLastTouchDownIndex = childIndex;
}
mLastTouchDownX = ev.getX();
mLastTouchDownY = ev.getY();
//3.1 true,若是处理了事件,则赋值mFirstTouchTarget,跳出循环.
newTouchTarget = addTouchTarget(child, idBitsToAssign);
alreadyDispatchedToNewTouchTarget = true;
break;
}
ev.setTargetAccessibilityFocus(false);
}
if (preorderedList != null) preorderedList.clear();
}
if (mFirstTouchTarget == null) { //3.2 无子view或者false没有处理事件
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
}
//省略部分代码
//dispatchTransformedTouchEvent方法
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
final boolean handled;
final int oldAction = event.getAction();
if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {
event.setAction(MotionEvent.ACTION_CANCEL);
if (child == null) {
//本身处理事件
handled = super.dispatchTouchEvent(event);
} else {
//调用子View的dispatchTouchEvent
handled = child.dispatchTouchEvent(event);
}
event.setAction(oldAction);
return handled;
}
复制代码
具体流程能够看注释,这里作个小结.
View的处理过程
对应源码
public boolean dispatchTouchEvent(MotionEvent event) {
//...省略部分代码
boolean result = false;
if (onFilterTouchEventForSecurity(event)) {
if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
result = true;
}
//noinspection SimplifiableIfStatement
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnTouchListener != null
&& (mViewFlags & ENABLED_MASK) == ENABLED
&& li.mOnTouchListener.onTouch(this, event)) {
result = true;
}
if (!result && onTouchEvent(event)) {//onTouch返回false时
result = true;
}
}
return result;
}
复制代码
最后对上面流程作一个小结.
因而可知优先级为: OnTouch > onTouchEvent > Onclcik。
因为本人技术有限,若有错误的地方,麻烦你们给我提出来,本人不胜感激,你们一块儿学习进步.
参考连接: