布局原理介绍
Android中四大组件中Activity组件就是呈现的许多页面,其中Activity中嵌套很多控件,其中有RelativeLayout、ImageView等等,这些控件的基类都是View这个类,当然我们也知道ViewGroup继承View这个类,它可以看做一个控件的容器,比如RelativeLayout。
触摸事件的类型
接下来就看看触摸事件的类型
ACTION_DOWN:用户手指按下操作,往往也代表着一次触摸事件的开始。
ACTION_MOVE:用户手指在屏幕上移动,一般情况下的轻微移动都会触发一系列的移动事件。
ACTION_POINTER_DOWN:额外的手指按下操作。
ACTION_POINTER_UP:额外的手指的离开操作
ACTION_UP:用户手指离开屏幕的操作,一次抬起操作标志着一次触摸事件的结束
了解完这些触摸事件类型,接下来就来看看触摸事件的原理,它是怎么实现的
首先我们点击手机屏幕,假如点击的地方是一个嵌套的View,消息机制是怎么传递的呢?
MyView.java
public class MyView extends View {
private static final String TAG = "MyView";
public MyView(Context context) {
super(context);
}
public MyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "dispatchTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "dispatchTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "dispatchTouchEvent: ACTION_UP");
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "onTouchEvent: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "onTouchEvent: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "onTouchEvent: ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
private static final String TAG = "MainActivity";
private MyView myView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView = (MyView) findViewById(R.id.myView);
myView.setOnClickListener(this);
myView.setOnTouchListener(this);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "dispatchTouchEvent: MainActivity ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "dispatchTouchEvent: MainActivity ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "dispatchTouchEvent: MainActivity ACTION_UP");
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "onTouchEvent: MainActivity ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "onTouchEvent: MainActivity ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "onTouchEvent: MainActivity ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "onTouch: myView ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "onTouch: myView ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "onTouch: myView ACTION_UP");
break;
}
return false;
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.myView){
Log.i(TAG, "onClick: myView");
}
}
}
点击MyView区域
MainActivity: dispatchTouchEvent: MainActivity ACTION_DOWN
MyView: dispatchTouchEvent: ACTION_DOWN
MainActivity: onTouch: myView ACTION_DOWN
MyView: onTouchEvent: ACTION_DOWN
MainActivity: dispatchTouchEvent: MainActivity ACTION_UP
MyView: dispatchTouchEvent: ACTION_UP
MainActivity: onTouch: myView ACTION_UP
MyView: onTouchEvent: ACTION_UP
MainActivity: onClick: myView
由输出结果可知,dispatchTouchEvent消息是由外向里传递,onTouchEvent处理这个消息,其返回结果就是该消息是否被消费,super则是被消费。如果不消费则把事件返回到父级消费。
把MyView中onTouchEvent返回之改为false
MainActivity: dispatchTouchEvent: MainActivity ACTION_DOWN
MyView: dispatchTouchEvent: ACTION_DOWN
MainActivity: onTouch: myView ACTION_DOWN
MyView: onTouchEvent: ACTION_DOWN
MainActivity: onTouchEvent: MainActivity ACTION_DOWN
MainActivity: dispatchTouchEvent: MainActivity ACTION_UP
MainActivity: onTouchEvent: MainActivity ACTION_UP
下面在Activity中添加一个Layout布局,其中View就在这个布局上
MyLayout.java
public class MyLayout extends RelativeLayout {
private static final String TAG = "MyLayout";
public MyLayout(Context context) {
super(context);
}
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "dispatchTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "dispatchTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "dispatchTouchEvent ACTION_UP");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(TAG, "dispatchTouchEvent ACTION_CANCEL");
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "onInterceptTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "onInterceptTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "onInterceptTouchEvent ACTION_UP");
break;
default:
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "onTouchEvent ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG, "onTouchEvent ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "onTouchEvent ACTION_UP");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(TAG, "onTouchEvent ACTION_CANCEL");
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
onInterceptTouchEvent方式ViewGroup中特有的方法,意思很简单它的返回值就是决定是否拦截此触摸活动,点击View区域来看看输出结果。
dispatchTouchEvent: MainActivity ACTION_DOWN
MyLayout: dispatchTouchEvent ACTION_DOWN
MyLayout: onInterceptTouchEvent ACTION_DOWN
MyLayout: onTouchEvent ACTION_DOWN
MainActivity: onTouchEvent: MainActivity ACTION_DOWN
MainActivity: dispatchTouchEvent: MainActivity ACTION_UP
MainActivity: onTouchEvent: MainActivity ACTION_UP
由此结果可知,dispatchTouchEvent并没有传递到View,当然这个拦截的效果,再将返回值改为false
MainActivity: dispatchTouchEvent: MainActivity ACTION_DOWN
MyLayout: dispatchTouchEvent ACTION_DOWN
MyLayout: onInterceptTouchEvent ACTION_DOWN
MyView: dispatchTouchEvent: ACTION_DOWN
MainActivity: onTouch: myView ACTION_DOWN
MyView: onTouchEvent: ACTION_DOWN
MainActivity: dispatchTouchEvent: MainActivity ACTION_UP
MyLayout: dispatchTouchEvent ACTION_UP
MyLayout: onInterceptTouchEvent ACTION_UP
MyView: dispatchTouchEvent: ACTION_UP
MainActivity: onTouch: myView ACTION_UP
MyView: onTouchEvent: ACTION_UP
MainActivity: onClick: myView
结合onTouchEvent的讲解输出结果一目了然了。
源码分析
通过对源码的具体分析来验证以上的输出结果
View.java
public boolean dispatchTouchEvent(MotionEvent event) {
....
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)) {
result = true;
}
}
....
}
由上面代码逻辑可以知道首先执行li.mOnTouchListener.onTouch(this, event),mOnTouchListener就是在Activity中设置的监听回调,如果onTouch返回值为true,显而易见dispatchTouchEvent就直接返回了不会往后执行onTouchEvent这个函数了。这也是为什么输出结果先执行onTouch而后执行onTouchEvent得原因,也是onTouch返回false,而执行onTouchEvent。
接下来看onTouchEvent
public boolean onTouchEvent(MotionEvent event) {
...
if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
switch (action) {
case MotionEvent.ACTION_UP:
...
if (!mHasPerformedLongPress && !mIgnoreNextUpEvent) {
// This is a tap, so remove the longpress check
removeLongPressCallback();
// Only perform take click actions if we were in the pressed state
if (!focusTaken) {
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
}
}
...
}
}
mPerformClick 这个函数实现Runnable这个接口,回调li.mOnClickListener.onClick(this)方法。这也是onClick在ACTION_UP时最后调用的原因