Android touch event delivery mechanism

Android touch event delivery mechanism

A complete event delivery mainly includes three stages, namely event distribution, interception and consumption.< /p>

1.1 Types of touch events

Touch events correspond to the MotionEvent class. There are three main types of events as follows:

  1>.ACTION_DOWN: The user’s finger Press operation, a press operation marks the beginning of a touch event.

  2>.ACTION_MOVE: After the user’s finger presses the screen, before releasing it, if the moving distance exceeds a certain threshold (yu ) Value, then it will be judged as an ACTION_MOVE operation. Under normal circumstances, a slight movement of the finger will trigger a series of movement events.

  3>.ACTION_UP: The operation of the user’s finger leaving the screen, one lift The operation marks the end of a touch event.

   In a screen touch operation, the two events ACTION_DOWN and ACTION_UP are necessary, and ACTION_MOVE depends on the situation, if the user just clicks on the screen Then it may only detect the actions of pressing and lifting.

1.2 Three stages of event delivery

  1. Dispatch

   of events Distribution corresponds to the dispatchTouchEvent method. In the Android system, all touch events are distributed through this method.

  

 public boolean dispatchTouchEvent(MotionEvent ev) {

return super.dispatchTouchEvent(ev);
}

The method returns true to indicate that the event is consumed by the current view and no longer distributes the event; the method returns super.dispatchTouchEvent(ev) to indicate that the event continues to be distributed Event.

If the current view is ViewGroup and its subclasses, the onInterceptTouchEvent method will be called back to determine whether to intercept the event.

   2. Intercept (Intercept)

The interception of    events corresponds to the onInterceptTouchEvent method, which only exists in ViewGroup and its subclasses, and does not exist in View and Activity.

  

 public boolean onInterceptTouchEvent(MotionEvent ev) {

return super.onInterceptTouchEvent(ev);
}

This method returns true to indicate that this event is intercepted, and it will not continue to be distributed to the subviews, and at the same time will be handed over to its own onTouchEvent method for consumption; return false and super .onInterceptTouchEvent(ev) means that the event is not intercepted and needs to continue to be passed to the subview.

  3.Consume (Consume)

  The consumption of the event corresponds to onTouchEvent method

public boolean onTouchEvent(MotionEvent event) {

return super.onTouchEvent(event);
}

The return value of this method is true, which means that the current view can handle the corresponding event, and the event will not be passed up to the parent view; the return value is false, which means The current view does not handle this event, and the event will be passed to the onTouchEvent method of the parent view for processing.

In the Android system, there are three classes that have event delivery processing capabilities. Kind:

  1>.Activity: has dispatchTouchEvent and onTouchEvent two methods.

  2>.ViewGroup: has dispatchTouchEvent, onInterceptTouchEvent and onTouchEvent three methods.

  3>.View: has dispatchTouchEvent and onTouchEvent two methods.

1.3 View event delivery mechanism

   View control has dispatchTouchEvent and onTouchEvent two methods, defining a class that inherits TextView And MyTextView, as shown below: We will log the triggering event of each event to facilitate the understanding of the delivery process of the event.

public class MyTextView extends TextView {


private static final String TAG ="MyTextView";

public MyTextView(Context context) {
super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.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(event);
}

@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);
}
}

At the same time, define a MainActivity to display MyTextView. In this Activity, click (onClick) and Touch (onTouch) monitoring to facilitate tracking and understanding of the process of event delivery:

public class MainActivity extends AppCompatActivity implements< span style="color: #000000;"> View.OnClickListener, View.OnTouchListener {


private static final String TAG ="MainActivity";
private MyTextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView
=findViewById(R.id.my_text);
mTextView.setOnClickListener(
this);
mTextView.setOnTouchListener(
this);
}

@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 onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.my_text:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG,
"MyTextView onTouch: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG,
"MyTextView onTouch: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG,
"MyTextView onTouch: ACTION_UP");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(TAG,
"MyTextView onTouch: ACTION_CANCEL");
break;
default:
break;
}
break;
default:
break;
}
return false;
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.my_text:
Log.e(TAG,
"MyTextView onClick: ");
break;
default:
break;
}
}
}

Run the above code, click MyTextView, and the following log will be printed in Logcat:

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 20:25:01.833 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onClick:

Press and press to lift MyTextView, The following log will be printed in Logcat:

09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_MOVE
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onClick:

From the above running log, you can It can be seen that there are three situations in the return value of the two methods of dispatchTouchEvent and onTouchEvent:

  1>.return false;

    

09-02 20:46:38.013 21590-21590/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:46:38.103 21590-21590/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP

2>. return true;

09-02 20:45:23.733 20676-20676/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:45:23.753 20676-20676/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP

  3>. Return to the parent class Same name method return super.dispatchTouchEvent(ev);

  

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E /MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 20:25:01.833 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onClick:

Different return values ​​will lead to a very different event delivery process. By continuously modifying the return values ​​of these methods to view the log records; the following conclusions can be drawn:

1>. The transfer process of touch events starts from dispatchTouchEvent. If there is no human intervention (that is, the function of the same name of the parent class is returned by default), the event will follow the nesting layer to the outer layer When it reaches the innermost View, it is processed by its onTouchEvent method,

   then The returned true, if it can’t be processed, how to return false, then the event will be re-transmitted to the outer layer and processed by the onTouchEvent method of the outer View, and so on.

2>. If the event is passed to the inner layer due to human intervention, the event processing function returns true, which will cause the event to be consumed in advance. Layer View will not receive this event.

3>. View control’s event trigger sequence The onTouch method is executed first, and the onClick method is executed at the end. If onTouch returns true, the event will not continue to be delivered, and the onClick method will not be called at the end; if onTouch returns false, the event will continue to be delivered.

1.4. ViewGroup event delivery mechanism

ViewGroup has dispatchTouchEvent, onInterceptTouchEvent and onTouchEvent Three methods.

It can be seen that the only difference with View is the addition of an onInterceptTouchEvent method; demo , We define a ViewGroup as follows, inherit RelativeLayout, and implement a MyRelativeLayout .

public class MyRelativeLayout extends RelativeLayout {


private static final String TAG ="MyRelativeLayout";

public MyRelativeLayout(Context context) {
super(context);
}

public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.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(event);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.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;
case MotionEvent.ACTION_CANCEL:
Log.e(TAG,
"onInterceptTouchEvent: ACTION_CANCEL");
break;
default:
break;
}
return super.onInterceptTouchEvent(event);
}

@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);
}
}

xml version="1.0" encoding="utf-8"?>

<com.kitking.androidtest.event.MyRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent">


<com.kitking.androidtest.event.MyTextView
android:id="@+id/my_text"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="30dp"
android:text
="Event"/>

com.kitking.androidtest.event.MyRelativeLayout>

运行,点击MyTextView,在Logcat中打印日志如下:

09-02 21:01:07.813 25884-25884/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyRelativeLayout: dispatchTouchEvent: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyRelativeLayout: onInterceptTouchEvent: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyRelativeLayout: dispatchTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyRelativeLayout: onInterceptTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 21:01:07.853 25884-25884/com.kitking.androidtest E/MainActivity: MyTextView onClick:

可以看到,与View的事件流程唯一不一样的地方是在MainActivity和MyTextView之间增加了MyRelativeLayout,同理通过打印日志可以获得以下结论:

1>. 触摸事件的传递顺序由Activity到ViewGroup,在由ViewGroup传递给它的子类View

2>. ViewGroup通过dispatchTouchEvent方法对事件进行拦截,如果该方法返回true,则事件不会继续传给子View,如果返回false或者super.dispatchTouchEvent,则事件会继续传递给子View.

3>. 在子View对事件进行消费后,ViewGroup将接收不到任何事件.

 public boolean dispatchTouchEvent(MotionEvent ev) {

return super.dispatchTouchEvent(ev);
}

 public boolean onInterceptTouchEvent(MotionEvent ev) {

return super.onInterceptTouchEvent(ev);
}

public boolean onTouchEvent(MotionEvent event) {

return super.onTouchEvent(event);
}

public class MyTextView extends TextView {


private static final String TAG ="MyTextView";

public MyTextView(Context context) {
super(context);
}

public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.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(event);
}

@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);
}
}

public class MainActivity extends AppCompatActivity implements View.OnClickListener , View.OnTouchListener {


private static final String TAG ="MainActivity";
private MyTextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView
=findViewById(R.id.my_text);
mTextView.setOnClickListener(
this);
mTextView.setOnTouchListener(
this);
}

@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 onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.my_text:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG,
"MyTextView onTouch: ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.e(TAG,
"MyTextView onTouch: ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.e(TAG,
"MyTextView onTouch: ACTION_UP");
break;
case MotionEvent.ACTION_CANCEL:
Log.e(TAG,
"MyTextView onTouch: ACTION_CANCEL");
break;
default:
break;
}
break;
default:
break;
}
return false;
}

@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.my_text:
Log.e(TAG,
"MyTextView onClick: ");
break;
default:
break;
}
}
}

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 20:25:01.833 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onClick:

09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 20:33:50.583 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_MOVE
09-02 20:33:50.643 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_MOVE
09-02 20:33:50.673 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_MOVE
09-02 20:33:51.173 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 20:33:51.183 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onClick:

09-02 20:46:38.013 21590-21590/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:46:38.103 21590-21590/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP

09-02 20:45:23.733 20676-20676/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:45:23.753 20676-20676/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 20:25:01.783 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 20:25:01.833 15374-15374/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 20:25:01.843 15374-15374/com.kitking.androidtest E/MainActivity: MyTextView onClick:

public class MyRelativeLayout extends RelativeLayout {


private static final String TAG ="MyRelativeLayout";

public MyRelativeLayout(Context context) {
super(context);
}

public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.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(event);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.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;
case MotionEvent.ACTION_CANCEL:
Log.e(TAG,
"onInterceptTouchEvent: ACTION_CANCEL");
break;
default:
break;
}
return super.onInterceptTouchEvent(event);
}

@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);
}
}

xml version="1.0" encoding="utf-8"?>

<com.kitking.androidtest.event.MyRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="match_parent"
android:layout_height
="match_parent">


<com.kitking.androidtest.event.MyTextView
android:id="@+id/my_text"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="30dp"
android:text
="Event"/>

com.kitking.androidtest.event.MyRelativeLayout>

09-02 21:01:07.813 25884-25884/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_DOWN

09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyRelativeLayout: dispatchTouchEvent: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyRelativeLayout: onInterceptTouchEvent: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_DOWN
09-02 21:01:07.823 25884-25884/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_DOWN
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MainActivity: dispatchTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyRelativeLayout: dispatchTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyRelativeLayout: onInterceptTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyTextView: dispatchTouchEvent: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MainActivity: MyTextView onTouch: ACTION_UP
09-02 21:01:07.833 25884-25884/com.kitking.androidtest E/MyTextView: onTouchEvent: ACTION_UP
09-02 21:01:07.853 25884-25884/com.kitking.androidtest E/MainActivity: MyTextView onClick:

Leave a Comment

Your email address will not be published.