MotionEvent对象

来源:互联网 发布:淘宝消费群体分析 编辑:程序博客网 时间:2024/05/17 22:21
当用户触摸屏幕时将创建一个MotionEvent对象。MotionEvent包含关于发生触摸的位置和时间等细节信息。MotionEvent对象被传递到程序中合适的方法比如View对象的onTouchEvent()方法中。在这些方法中我们可以分析MotionEvent对象那个,以决定要执行的操作。

   MotionEvent对象是与用户触摸相关的时间序列,该序列从用户首次触摸屏幕开始,经历手指在屏幕表面的任何移动,直到手指离开屏幕时结束。手指的初次触摸(ACTION_DOWN操作),滑动(ACTION_MOVE操作)和抬起(ACTION_UP)都会创建MotionEvent对象。移动过程中会产生大量事件,每个事件都会产生对应的MotionEvent对象记录发生的操作,触摸的位置,使用的多大压力,触摸的面积,合适发生,以及最初的ACTION_DOWN和时发生等相关的信息。

    在设置事件时我们有2中设置的方式,一种是委托式一种是回调式。第一种就是将事件的处理委托给监听器处理,你可以定义一个View.OnTouchListener接口的子类作为监听器,其中有onTouch()方法。而第二种是重写View类自己本身的onTouchEvent方法,也就是控件自己处理事件。onTouch方法接收一个MotionEvent参数和一个View参数,而onTouchEvent方法仅接收MotionEvent参数。这是因为监听器可以监听多个View控件的事件。无论是通过onTouchEvent还是onTouch方法 它们的返回值都是boolean类型。true的含义是如果当前处理程序在处理完毕该事件后不希望传播给其他控件,则返回true。如果View对象不但对此事件不感兴趣,而且对与此触摸序列相关的任何未来事件都不感兴趣,那么返回false。比如如果Button的onTouchEvent方法想要处理用户的一次点击 则会有2个事件产生ACTION_DOWN和ACTION_UP,按道理这两个事件都会调用onTouchEvent方法,如果方法返回false则在按下时你可以做一些操作,但是手指抬起时你将不能再接收到MotionEvent对象了,所以你也就无从处理抬起事件了。

[java] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.util.Log;  
  4. import android.view.GestureDetector;  
  5. import android.view.GestureDetector.OnGestureListener;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnTouchListener;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class GestureActivity extends Activity implements OnTouchListener,  
  13.         OnGestureListener {  
  14.   
  15.     GestureDetector detector;  
  16.   
  17.     public GestureActivity() {  
  18.         detector = new GestureDetector(this);  
  19.     }  
  20.       
  21.     public void onCreate(Bundle savedInstanceState) {    
  22.             super.onCreate(savedInstanceState);    
  23.             setContentView(R.layout.main);    
  24.             TextView tv = (TextView) findViewById(R.id.TextView001);  
  25.             //设置tv的监听器     
  26.             tv.setOnTouchListener(this);    
  27.             tv.setFocusable(true);  
  28.             //必须,view才能够处理不同于Tap(轻触)的hold   
  29.             tv.setClickable(true);    
  30.             tv.setLongClickable(true);    
  31.             detector.setIsLongpressEnabled(true);    
  32.     }    
  33.       
  34.       
  35.     /*  
  36.      * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector  
  37.      * 来分析是否有合适的callback函数来处理用户的手势  
  38.      */     
  39.     public boolean onTouch(View v, MotionEvent event) {    
  40.         return detector.onTouchEvent(event);    
  41.     }    
  42.     
  43.     // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发    
  44.     public boolean onDown(MotionEvent arg0) {    
  45.         Log.i("MyGesture""onDown");    
  46.         Toast.makeText(this"onDown", Toast.LENGTH_SHORT).show();    
  47.         return true;    
  48.     }    
  49.         
  50.     /*  
  51.      * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
  52.      * 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
  53.      */    
  54.     public void onShowPress(MotionEvent e) {    
  55.         Log.i("MyGesture""onShowPress");    
  56.         Toast.makeText(this"onShowPress", Toast.LENGTH_SHORT).show();    
  57.     }    
  58.         
  59.     // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发    
  60.     public boolean onSingleTapUp(MotionEvent e) {    
  61.         Log.i("MyGesture""onSingleTapUp");    
  62.         Toast.makeText(this"onSingleTapUp", Toast.LENGTH_SHORT).show();    
  63.         return true;    
  64.     }    
  65.         
  66.     // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发    
  67.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {    
  68.         Log.i("MyGesture""onFling");    
  69.           
  70.         // 参数解释:     
  71.         // e1:第1个ACTION_DOWN MotionEvent    
  72.         // e2:最后一个ACTION_MOVE MotionEvent    
  73.         // velocityX:X轴上的移动速度,像素/秒    
  74.         // velocityY:Y轴上的移动速度,像素/秒     
  75.         
  76.         // 触发条件 :     
  77.         // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒    
  78.             
  79.         final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;    
  80.         if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  81.             // Fling left     
  82.             Log.i("MyGesture""Fling left");    
  83.             Toast.makeText(this"Fling Left", Toast.LENGTH_SHORT).show();    
  84.         } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  85.             // Fling right     
  86.             Log.i("MyGesture""Fling right");    
  87.             Toast.makeText(this"Fling Right", Toast.LENGTH_SHORT).show();    
  88.         } else if(e2.getY()-e1.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {  
  89.             // Fling down     
  90.             Log.i("MyGesture""Fling down");    
  91.             Toast.makeText(this"Fling down", Toast.LENGTH_SHORT).show();  
  92.         } else if(e1.getY()-e2.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {  
  93.             // Fling up     
  94.             Log.i("MyGesture""Fling up");    
  95.             Toast.makeText(this"Fling up", Toast.LENGTH_SHORT).show();  
  96.         }    
  97.           
  98.           
  99.         return false;   
  100.            
  101.     }    
  102.         
  103.     // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发    
  104.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {    
  105.         Log.i("MyGesture""onScroll");    
  106.         Toast.makeText(this"onScroll", Toast.LENGTH_LONG).show();    
  107.         return true;    
  108.     }    
  109.         
  110.     // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发    
  111.     public void onLongPress(MotionEvent e) {    
  112.         Log.i("MyGesture""onLongPress");    
  113.         Toast.makeText(this"onLongPress", Toast.LENGTH_LONG).show();    
  114.     }    
  115.       
  116.   
  117. }  
0 0
原创粉丝点击