关于motionevent的使用

小菜鸟战斗机 2018-3-27 200

[html] view plain copy
  1. 最近在研究robotium的方法,在Scroller.java中有一个drag方法,如下:  

[java] view plain copy
  1. /** 
  2.      * Simulate touching a specific location and dragging to a new location. 
  3.      * 
  4.      * This method was copied from {@code TouchUtils.java} in the Android Open Source Project, and modified here. 
  5.      * 
  6.      * @param fromX X coordinate of the initial touch, in screen coordinates 
  7.      * @param toX Xcoordinate of the drag destination, in screen coordinates 
  8.      * @param fromY X coordinate of the initial touch, in screen coordinates 
  9.      * @param toY Y coordinate of the drag destination, in screen coordinates 
  10.      * @param stepCount How many move steps to include in the drag 
  11.      */  
  12.   
  13.     public void drag(float fromX, float toX, float fromY, float toY,  
  14.             int stepCount) {  
  15.         long downTime = SystemClock.uptimeMillis();  
  16.         long eventTime = SystemClock.uptimeMillis();  
  17.         float y = fromY;  
  18.         float x = fromX;  
  19.         float yStep = (toY - fromY) / stepCount;  
  20.         float xStep = (toX - fromX) / stepCount;  
  21.         <strong><span style="color:#ff6666;">MotionEvent event = MotionEvent.obtain(downTime, eventTime,MotionEvent.ACTION_DOWN, fromX, fromY, 0);</span></strong>  
  22.         try {  
  23.             inst.sendPointerSync(event);  
  24.         } catch (SecurityException ignored) {}  
  25.         for (int i = 0; i < stepCount; ++i) {  
  26.             y += yStep;  
  27.             x += xStep;  
  28.             eventTime = SystemClock.uptimeMillis();  
  29.             event = MotionEvent.obtain(downTime, eventTime,MotionEvent.ACTION_MOVE, x, y, 0);  
  30.             try {  
  31.                 inst.sendPointerSync(event);  
  32.             } catch (SecurityException ignored) {}  
  33.         }  
  34.         eventTime = SystemClock.uptimeMillis();  
  35.         event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP,toX, toY, 0);  
  36.         try {  
  37.             inst.sendPointerSync(event);  
  38.         } catch (SecurityException ignored) {}  
  39.     }  

里面有个MotionEvent的类,下面摘自网上的一篇博客,写的很好;


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解。
一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP
当屏幕中包含一个ViewGroup,而这个ViewGroup又包含一个子view,这个时候android系统如何处理Touch事件呢?到底是ViewGroup来处理Touch事件,还是子view来处理Touch事件呢?我只能很肯定的对你说不一定。呵呵,为什么呢?看看下面我的调查结果你就明白了。
android系统中的每个View的子类都具有下面三个和TouchEvent处理密切相关的方法:
1)public boolean dispatchTouchEvent(MotionEvent ev)  这个方法用来分发TouchEvent
2)public boolean onInterceptTouchEvent(MotionEvent ev) 这个方法用来拦截TouchEvent
3)public boolean onTouchEvent(MotionEvent ev) 这个方法用来处理TouchEvent
当TouchEvent发生时,首先Activity将TouchEvent传递给最顶层的View, TouchEvent最先到达最顶层 view 的 dispatchTouchEvent ,然后由  dispatchTouchEvent 方法进行分发,如果dispatchTouchEvent返回true ,则交给这个view的onTouchEvent处理,如果dispatchTouchEvent返回 false ,则交给这个 view 的 interceptTouchEvent 方法来决定是否要拦截这个事件,如果 interceptTouchEvent 返回 true ,也就是拦截掉了,则交给它的 onTouchEvent 来处理,如果 interceptTouchEvent 返回 false ,那么就传递给子 view ,由子 view 的 dispatchTouchEvent 再来开始这个事件的分发。如果事件传递到某一层的子 view 的 onTouchEvent 上了,这个方法返回了 false ,那么这个事件会从这个 view 往上传递,都是 onTouchEvent 来接收。而如果传递到最上面的 onTouchEvent 也返回 false 的话,这个事件就会“消失”,而且接收不到下一次事件。
通过语言描述这个处理逻辑很抽象,下面我就用代码来具体说明一下。
layout配置文件 main.xml


[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <test.lzqdiy.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <test.lzqdiy.MyTextView  
  9.         android:id="@+id/tv"  
  10.         android:layout_width="200px"  
  11.         android:layout_height="200px"  
  12.         android:background="#FFFFFF"  
  13.         android:text="lzqdiy"  
  14.         android:textColor="#0000FF"  
  15.         android:textSize="40sp"  
  16.         android:textStyle="bold" />  
  17.   
  18. </test.lzqdiy.MyLinearLayout>  


三个java文件:

[java] view plain copy
  1. package test.lzqdiy;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6.   
  7. public class MainActivity extends Activity  
  8. {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState)  
  12.     {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.     }  
  16.   
  17.     @Override  
  18.     public boolean onCreateOptionsMenu(Menu menu)  
  19.     {  
  20.         // Inflate the menu; this adds items to the action bar if it is present.  
  21.         getMenuInflater().inflate(R.menu.main, menu);  
  22.         return true;  
  23.     }  
  24.   
  25. }  


[java] view plain copy
  1. package test.lzqdiy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.LinearLayout;  
  8.   
  9. public class MyLinearLayout extends LinearLayout {  
  10.     private final String TAG = "MyLinearLayout";  
  11.   
  12.     public MyLinearLayout(Context context, AttributeSet attrs) {  
  13.   
  14.         super(context, attrs);  
  15.   
  16.         Log.d(TAG, TAG);  
  17.   
  18.     }  
  19.   
  20.     @Override  
  21.     public boolean dispatchTouchEvent(MotionEvent ev) {  
  22.         int action = ev.getAction();  
  23.   
  24.         switch (action) {  
  25.   
  26.         case MotionEvent.ACTION_DOWN:  
  27.   
  28.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  29.   
  30.             break;  
  31.   
  32.         case MotionEvent.ACTION_MOVE:  
  33.   
  34.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  35.   
  36.             break;  
  37.   
  38.         case MotionEvent.ACTION_UP:  
  39.   
  40.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  41.   
  42.             break;  
  43.   
  44.         case MotionEvent.ACTION_CANCEL:  
  45.   
  46.             Log.d(TAG, "dispatchTouchEvent action:ACTION_CANCEL");  
  47.   
  48.             break;  
  49.   
  50.         }  
  51.         return super.dispatchTouchEvent(ev);  
  52.     }  
  53.   
  54.     @Override  
  55.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  56.   
  57.         int action = ev.getAction();  
  58.   
  59.         switch (action) {  
  60.   
  61.         case MotionEvent.ACTION_DOWN:  
  62.   
  63.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_DOWN");  
  64.   
  65.             break;  
  66.   
  67.         case MotionEvent.ACTION_MOVE:  
  68.   
  69.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_MOVE");  
  70.   
  71.             break;  
  72.   
  73.         case MotionEvent.ACTION_UP:  
  74.   
  75.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_UP");  
  76.   
  77.             break;  
  78.   
  79.         case MotionEvent.ACTION_CANCEL:  
  80.   
  81.             Log.d(TAG, "onInterceptTouchEvent action:ACTION_CANCEL");  
  82.   
  83.             break;  
  84.   
  85.         }  
  86.   
  87.         return false;  
  88.   
  89.     }  
  90.   
  91.     @Override  
  92.     public boolean onTouchEvent(MotionEvent ev) {  
  93.   
  94.         int action = ev.getAction();  
  95.   
  96.         switch (action) {  
  97.   
  98.         case MotionEvent.ACTION_DOWN:  
  99.   
  100.             Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");  
  101.   
  102.             break;  
  103.   
  104.         case MotionEvent.ACTION_MOVE:  
  105.   
  106.             Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");  
  107.   
  108.             break;  
  109.   
  110.         case MotionEvent.ACTION_UP:  
  111.   
  112.             Log.d(TAG, "---onTouchEvent action:ACTION_UP");  
  113.   
  114.             break;  
  115.   
  116.         case MotionEvent.ACTION_CANCEL:  
  117.   
  118.             Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");  
  119.   
  120.             break;  
  121.   
  122.         }  
  123.   
  124.         return true;  
  125.     }  
  126.   
  127. }  


[java] view plain copy
  1. package test.lzqdiy;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.util.Log;  
  6. import android.view.MotionEvent;  
  7. import android.widget.TextView;  
  8.   
  9. public class MyTextView extends TextView  
  10. {  
  11.   
  12.     private final String TAG = "MyTextView";  
  13.   
  14.     public MyTextView(Context context, AttributeSet attrs)  
  15.     {  
  16.   
  17.         super(context, attrs);  
  18.   
  19.     }  
  20.   
  21.     @Override  
  22.     public boolean dispatchTouchEvent(MotionEvent ev)  
  23.     {  
  24.         int action = ev.getAction();  
  25.   
  26.         switch (action)  
  27.         {  
  28.   
  29.         case MotionEvent.ACTION_DOWN:  
  30.   
  31.             Log.d(TAG, "dispatchTouchEvent action:ACTION_DOWN");  
  32.   
  33.             break;  
  34.   
  35.         case MotionEvent.ACTION_MOVE:  
  36.   
  37.             Log.d(TAG, "dispatchTouchEvent action:ACTION_MOVE");  
  38.   
  39.             break;  
  40.   
  41.         case MotionEvent.ACTION_UP:  
  42.   
  43.             Log.d(TAG, "dispatchTouchEvent action:ACTION_UP");  
  44.   
  45.             break;  
  46.   
  47.         case MotionEvent.ACTION_CANCEL:  
  48.   
  49.             Log.d(TAG, "onTouchEvent action:ACTION_CANCEL");  
  50.   
  51.             break;  
  52.   
  53.         }  
  54.         return super.dispatchTouchEvent(ev);  
  55.     }  
  56.   
  57.     @Override  
  58.     public boolean onTouchEvent(MotionEvent ev)  
  59.     {  
  60.   
  61.         int action = ev.getAction();  
  62.   
  63.         switch (action)  
  64.         {  
  65.   
  66.         case MotionEvent.ACTION_DOWN:  
  67.   
  68.             Log.d(TAG, "---onTouchEvent action:ACTION_DOWN");  
  69.   
  70.             break;  
  71.   
  72.         case MotionEvent.ACTION_MOVE:  
  73.   
  74.             Log.d(TAG, "---onTouchEvent action:ACTION_MOVE");  
  75.   
  76.             break;  
  77.   
  78.         case MotionEvent.ACTION_UP:  
  79.   
  80.             Log.d(TAG, "---onTouchEvent action:ACTION_UP");  
  81.   
  82.             break;  
  83.   
  84.         case MotionEvent.ACTION_CANCEL:  
  85.   
  86.             Log.d(TAG, "---onTouchEvent action:ACTION_CANCEL");  
  87.   
  88.             break;  
  89.   
  90.         }  
  91.   
  92.         return true;  
  93.   
  94.     }  
  95.   
  96. }  


为了指代方便,下面将MyLinearLayout简称为L,将MyTextView简称为T,L.onInterceptTouchEvent=true 表示的含义为MyLinearLayout中的onInterceptTouchEvent方法返回值为true,通过程序运行时输出的Log来说明调用时序。
第1种情况 L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=true 输出下面的Log:
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(11865): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(11865): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_MOVE
D/MyTextView(11865): dispatchTouchEvent action:ACTION_MOVE
D/MyTextView(11865): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(11865): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(11865): onInterceptTouchEvent action:ACTION_UP
D/MyTextView(11865): dispatchTouchEvent action:ACTION_UP
D/MyTextView(11865): ---onTouchEvent action:ACTION_UP
结论:TouchEvent完全由TextView处理。
第2种情况  L.onInterceptTouchEvent=false&& L.onTouchEvent=true &&T.onTouchEvent=false 输出下面的Log:
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): onInterceptTouchEvent action:ACTION_DOWN
D/MyTextView(13101): dispatchTouchEvent action:ACTION_DOWN
D/MyTextView(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13101): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13101): ---onTouchEvent action:ACTION_UP
结论:TextView只处理了ACTION_DOWN事件,LinearLayout处理了所有的TouchEvent。
第3种情况  L.onInterceptTouchEvent=true&& L.onTouchEvent=true 输出下面的Log:
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): onInterceptTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_MOVE
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_MOVE
...........省略其他的ACTION_MOVE事件Log
D/MyLinearLayout(13334): dispatchTouchEvent action:ACTION_UP
D/MyLinearLayout(13334): ---onTouchEvent action:ACTION_UP
结论:LinearLayout处理了所有的TouchEvent。
第4种情况  L.onInterceptTouchEvent=true&& L.onTouchEvent=false 输出下面的Log:
D/MyLinearLayout(13452): dispatchTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13452): onInterceptTouchEvent action:ACTION_DOWN
D/MyLinearLayout(13452): ---onTouchEvent action:ACTION_DOWN
结论:LinearLayout只处理了ACTION_DOWN事件,那么其他的TouchEvent被谁处理了呢?答案是LinearLayout最外层的Activity处理了TouchEvent。


最新回复 (0)
返回