MotionEvent getx getRawx getTop

当用户触摸屏幕时将创建一个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对象了,所以你也就无从处理抬起事件了。


 

ACTION_MASK在Android中是应用于多点触摸操作,字面上的意思大概是动作掩码的意思吧。
在onTouchEvent(MotionEvent event)中,使用switch (event.getAction())可以处理ACTION_DOWN和ACTION_UP事件;使用switch (event.getAction() & MotionEvent.ACTION_MASK)就可以处理处理多点触摸的ACTION_POINTER_DOWN和ACTION_POINTER_UP事件。
CTION_DOWN和ACTION_UP就是单点触摸屏幕,按下去和放开的操作;
ACTION_POINTER_DOWN和ACTION_POINTER_UP就是多点触摸屏幕,当有一只手指按下去的时候,另一只手指按下和放开的动作捕捉;
ACTION_MOVE就是手指在屏幕上移动的操作;
常用的都是这几个吧。


Java代码   收藏代码
  1. public class Res extends Activity implements View.OnTouchListener {    
  2.     
  3.     Button btn = null;    
  4.     int x = 0;    
  5.     int y = 0;    
  6.     int rawx = 0;    
  7.     int rawy = 0;    
  8.     
  9.     /** Called when the activity is first created. */    
  10.     @Override    
  11.     public void onCreate(Bundle savedInstanceState) {    
  12.         super.onCreate(savedInstanceState);    
  13.         setContentView(R.layout.main);    
  14.         btn = (Button) findViewById(R.id.button);    
  15.         btn.setOnTouchListener(this);    
  16.     }    
  17.     
  18.     public boolean onTouch(View view, MotionEvent event) {    
  19.         int eventaction = event.getAction();    
  20.         switch (eventaction) {    
  21.         case MotionEvent.ACTION_DOWN:    
  22.             break;    
  23.         case MotionEvent.ACTION_MOVE:    
  24.             x = (int) event.getX();    
  25.             y = (int) event.getY();    
  26.             rawx = (int) event.getRawX();    
  27.             rawy = (int) event.getRawY();    
  28.             Log.d("DEBUG""getX=" + x + "getY=" + y + "\n" + "getRawX=" + rawx    
  29.                     + "getRawY=" + rawy + "\n");    
  30.             break;    
  31.     
  32.         case MotionEvent.ACTION_UP:    
  33.     
  34.             break;    
  35.         }    
  36.         return false;    
  37.     }    
  38. }    


结论:
当你触到按钮时,x,y是相对于该按钮左上点的相对位置。而rawx,rawy始终是相对于屏幕的位置。


另 如下图




可知:

getRowX:触摸点相对于屏幕的坐标
getX: 触摸点相对于按钮的坐标
getTop: 按钮左上角相对于父view(LinerLayout)的y坐标
getLeft: 按钮左上角相对于父view(LinerLayout)的x坐标

可以想象 getRight()等同于下面的计算:getLeft()+getWidth()。



MotionEvent事件在onInterceptTouchEvent()、onTouchEvent()中的传递顺序

onInterceptTouchEvent()用于处理事件并改变事件的传递方向。处理事件这个不用说了,你在函数内部编写代码处理就可以了。而决定传递方向的是返回值,返回为false时事件会传递给子控件的onInterceptTouchEvent();返回值为true时事件会传递给当前控件的onTouchEvent(),而不在传递给子控件,这就是所谓的Intercept(截断)。

onTouchEvent() 用于处理事件,返回值决定当前控件是否消费(consume)了这个事件。可能你要问是否消费了又区别吗,反正我已经针对事件编写了处理代码?答案是有区别!比如ACTION_MOVE或者ACTION_UP发生的前提是一定曾经发生了ACTION_DOWN,如果你没有消费ACTION_DOWN,那么系统会认为ACTION_DOWN没有发生过,所以ACTION_MOVE或者ACTION_UP就不能被捕获。

<?xml version="1.0" encoding="utf-8"?>
<com.touchstudy.LayoutView1 xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.touchstudy.LayoutView2
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center">
       <com.touchstudy.MyTextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv"
            android:text="AB"
            android:textSize="40sp"
            android:textStyle="bold"
            android:background="#FFFFFF"
            android:textColor="#0000FF"/>
   </com.touchstudy.LayoutView2>
</com.touchstudy.LayoutView1>

 

 

在没有重写onInterceptTouchEvent()和onTouchEvent()的情况下(他们的返回值都是false), 对上面这个布局,MotionEvent事件的传递顺序如下:

 

当某个控件的onInterceptTouchEvent()返回值为true时,就会发生截断,事件被传到当前控件的onTouchEvent()。如我们将LayoutView2的onInterceptTouchEvent()返回值为true,则传递流程变成:

 

 如果我们同时将LayoutView2的onInterceptTouchEvent()和onTouchEvent()设置成true,那么LayoutView2将消费被传递的事件,同时后续事件(如跟着ACTION_DOWN的ACTION_MOVE或者ACTION_UP)会直接传给LayoutView2的onTouchEvent(),不传给其他任何控件的任何函数。同时传递给子空间一个ACTION_CANCEL事件。传递流程变成(图中没有画出ACTION_CANCEL事件):

        

附SDK给出的说明:

·  You will receive the down event here.·  The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.·  For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().·  If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值