触摸事件类型
MotionEvent.ACTION_DOWN: 手指按下的操作
MotionEvent.ACTION_MOVE:手指头按下未抬起 并且有移动的操作
MotionEvent.ACTION_UP:手指离开手机屏幕的操作
1.以上操作 down 和 up 为一次完整的操作,是不唏嘘的,move看情况了。
事件传递的三个阶段
- 分发
对应的dispatchTouchEvent方法。所有触摸时间 通过这个方法开始分发。
返回 true 标识事件被消费掉,停止分发,如果当前试图为viewGroup或是她的子类,那么会调用
onInterceptTouchEvent 方法来判定是否拦截。 - 拦截
onInterceptTouchEvent 只存在于ViewGroup或者她的子类中,activity和其包含的view中均没有该方法。返回true 标识拦截时间 停止分发,不传递给子视图,交给自身的 onTouchEvent时间进行消费,返回false或者 super.onInterceptTouchEvent 标识不拦截,传递给子视图。
3.消费
onTouchEvent,该方法返回true 标识可以处理对应时间,时间不传递回给父视图。false标识传递给父视图的onTouchEvent进行处理
安卓系统中拥有事件处理能力的类:
- Activity:dispatchTouchEvent 和onTouchEvent 两个方法
- ViewGroup:dispatchTouchEvent 和onTouchEvent 和onInterceptTouchEvent 三个
- view:dispatchTouchEvent 和onTouchEvent 两个方法
View的事件传递机制
1.我们来自定义一个view如下:
/**
- Created by Administrator on 2018/1/8.
*/
public class MyTextview extends android.support.v7.widget.AppCompatTextView {
private static final String Tag = "MyTextview";
public MyTextview(Context context) {
super(context);
}
public MyTextview(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "dispatchTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "dispatchTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "dispatchTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "dispatchTouchEvent cacel");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "onTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "onTouchEvent cacel");
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
如上所示。。
2.新建一个activity 设计点击事件 和 触摸事件
public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {
private TextView view;
private static final String Tag = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.text_view);
view.setOnClickListener(this);
view.setOnTouchListener(this);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "dispatchTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "dispatchTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "dispatchTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "dispatchTouchEvent cacel");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "onTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "onTouchEvent cacel");
break;
default:
break;
}
return super.onTouchEvent(event);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.text_view:
Log.e(Tag, "onclick");
break;
default:
break;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (view.getId()) {
case R.id.text_view:
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "Mytextview onTouch down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "Mytextview onTouch move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "Mytextview onTouch up");
break;
default:
break;
}
break;
default:
break;
}
return false;
}
}
运行程序 打印log显示:
01-08 15:53:14.331 9124-9124/com.example.administrator.touchevent E/MainActivity: dispatchTouchEvent down
01-08 15:53:14.331 9124-9124/com.example.administrator.touchevent E/MyTextview: dispatchTouchEvent down
01-08 15:53:14.331 9124-9124/com.example.administrator.touchevent E/MainActivity: Mytextview onTouch down
01-08 15:53:14.331 9124-9124/com.example.administrator.touchevent E/MyTextview: onTouchEvent down
01-08 15:53:14.549 9124-9124/com.example.administrator.touchevent E/MainActivity: dispatchTouchEvent up
01-08 15:53:14.549 9124-9124/com.example.administrator.touchevent E/MyTextview: dispatchTouchEvent up
01-08 15:53:14.549 9124-9124/com.example.administrator.touchevent E/MainActivity: Mytextview onTouch up
01-08 15:53:14.549 9124-9124/com.example.administrator.touchevent E/MyTextview: onTouchEvent up
01-08 15:53:14.550 9124-9124/com.example.administrator.touchevent E/MainActivity: onclick
综上:
- 触摸事件 是从dispatchTouchEvent 开始,如果不进行人为干预的话事件会向内不传递,到达最内层的view是 由 本身onTouchEvent时间处理,如果该方法能消费掉改时间 返回true,负责返回 false,此时事件重新像3外层传递。并有外层的view的 onTouchEvent 处理,以此类推。
- 事件传递向内层过程中 提前被消费掉,则内层view无法收到这个事件。
- onTocuh方法执行完后 才会执行 onclick事件。
viewGroup的事件传递机制
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 event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "dispatchTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "dispatchTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "dispatchTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "dispatchTouchEvent cacel");
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "onInterceptTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "onInterceptTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "onInterceptTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "onInterceptTouchEvent cacel");
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(Tag, "onTouchEvent down");
break;
case MotionEvent.ACTION_MOVE:
Log.e(Tag, "onTouchEvent move");
break;
case MotionEvent.ACTION_UP:
Log.e(Tag, "onTouchEvent up");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(Tag, "onTouchEvent cacel");
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
将这个布局设为最外层布局,依旧打印log信息:
01-08 16:13:06.057 14539-14539/com.example.administrator.touchevent E/MainActivity: dispatchTouchEvent down
01-08 16:13:06.057 14539-14539/com.example.administrator.touchevent E/MyLayout: dispatchTouchEvent down
01-08 16:13:06.057 14539-14539/com.example.administrator.touchevent E/MyLayout: onInterceptTouchEvent down
01-08 16:13:06.057 14539-14539/com.example.administrator.touchevent E/MyTextview: dispatchTouchEvent down
01-08 16:13:06.057 14539-14539/com.example.administrator.touchevent E/MainActivity: Mytextview onTouch down
01-08 16:13:06.057 14539-14539/com.example.administrator.touchevent E/MyTextview: onTouchEvent down
01-08 16:13:06.143 14539-14539/com.example.administrator.touchevent E/MainActivity: dispatchTouchEvent up
01-08 16:13:06.143 14539-14539/com.example.administrator.touchevent E/MyLayout: dispatchTouchEvent up
01-08 16:13:06.143 14539-14539/com.example.administrator.touchevent E/MyLayout: onInterceptTouchEvent up
01-08 16:13:06.143 14539-14539/com.example.administrator.touchevent E/MyTextview: dispatchTouchEvent up
01-08 16:13:06.143 14539-14539/com.example.administrator.touchevent E/MainActivity: Mytextview onTouch up
01-08 16:13:06.143 14539-14539/com.example.administrator.touchevent E/MyTextview: onTouchEvent up
01-08 16:13:06.145 14539-14539/com.example.administrator.touchevent E/MainActivity: onclick
- 触摸事件传递顺序,Activity到viewGroup,再有ViewGropup传递到子view
- viewGroup 通过onInterceptTouchEvent进行拦截,返回 true 不会传递给 子view,false 则传递给子view
- 子view对事件进行消费后,viewGroup接收不到任何事件。
不要崇拜我。。。。。。。。哈哈哈哈哈哈。。。。。。。