MotionEvent Android API level 21

最近遇到一个跟MotionEvent相关的问题,顺便了解一下相关知识,理解不是很深刻,看到多少写多少吧,仅供参考。

public final class

MotionEvent

extends  InputEvent
implements  Parcelable
java.lang.Object
   ↳ android.view.InputEvent
     ↳ android.view.MotionEvent
MotionEvent主要是用来上报手指、触控板、鼠标等移动事件,并可以通过MotionEvent提供的相关方法获得与移动事件相关的参数。具体可以详见Google API reference和MotiionEvent source code。


常用的就一下几种事件:

/**
  * Constant for {@link #getActionMasked}: A pressed gesture has started, the
  * motion contains the initial starting location.
  * <p>
  * This is also a good time to check the button state to distinguish
  * secondary and tertiary button clicks and handle them appropriately.
  * Use {@link #getButtonState} to retrieve the button state.
  * </p>
  */
  public static final int ACTION_DOWN = 0;
   
  /**
  * Constant for {@link #getActionMasked}: A pressed gesture has finished, the
  * motion contains the final release location as well as any intermediate
  * points since the last down or move event.
  */
  public static final int ACTION_UP = 1;
   
  /**
  * Constant for {@link #getActionMasked}: A change has happened during a
  * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).
  * The motion contains the most recent point, as well as any intermediate
  * points since the last down or move event.
  */
  public static final int ACTION_MOVE = 2;
   
  /**
  * Constant for {@link #getActionMasked}: The current gesture has been aborted.
  * You will not receive any more points in it. You should treat this as
  * an up event, but not perform any action that you normally would.
  */
  public static final int ACTION_CANCEL = 3;
   
  /**
  * Constant for {@link #getActionMasked}: A movement has happened outside of the
  * normal bounds of the UI element. This does not provide a full gesture,
  * but only the initial location of the movement/touch.
  */
  public static final int ACTION_OUTSIDE = 4;
   
  /**
  * Constant for {@link #getActionMasked}: A non-primary pointer has gone down.
  * <p>
  * Use {@link #getActionIndex} to retrieve the index of the pointer that changed.
  * </p><p>
  * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the
  * unmasked action returned by {@link #getAction}.
  * </p>
  */
  public static final int ACTION_POINTER_DOWN = 5;
   
  /**
  * Constant for {@link #getActionMasked}: A non-primary pointer has gone up.
  * <p>
  * Use {@link #getActionIndex} to retrieve the index of the pointer that changed.
  * </p><p>
  * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the
  * unmasked action returned by {@link #getAction}.
  * </p>
  */
  public static final int ACTION_POINTER_UP = 6;

 
 

Google API 给出的关于ACTION_CANCEL这个事件的解释是你可以把它当做是ACTION_UP,但实际上什么事都不会做。实际使用中也极少使用到这个状态,通常都是与事件ACTION_UP做相同处理。印象里Google提供的触摸和按住延迟功能开启后才有ACTION_CANCEL事件上报用来取消操作。所以我们可以笼统的认为所有的触控事件都是以ACTION_DOWN开始以ACTION_UP。

快速点击事件:

04-30 17:36:54.795: D/MotionEventTest(12422): ACTION_DOWN
04-30 17:36:54.798: D/MotionEventTest(12422): ACTION_UP


长按事件:

04-30 17:38:43.222: D/MotionEventTest(12422): ACTION_DOWN
04-30 17:38:43.253: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:38:43.286: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:38:43.317: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:38:44.433: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:38:44.448: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:38:44.455: D/MotionEventTest(12422): ACTION_UP


移动事件:

04-30 17:40:56.330: D/MotionEventTest(12422): ACTION_DOWN
04-30 17:40:56.630: D/MotionEventTest(12422): ACTION_MOVE

很多个ACTION_MOVE事件

04-30 17:40:58.278: D/MotionEventTest(12422): ACTION_UP


多指触控事件:

04-30 17:42:45.080: D/MotionEventTest(12422): ACTION_DOWN
04-30 17:42:45.083: D/MotionEventTest(12422): ACTION_POINTER_DOWN 
04-30 17:42:45.102: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.168: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.185: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.202: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.607: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.624: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.641: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.646: D/MotionEventTest(12422): ACTION_POINTER_UP
04-30 17:42:45.961: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.968: D/MotionEventTest(12422): ACTION_MOVE
04-30 17:42:45.974: D/MotionEventTest(12422): ACTION_UP


Google API reference:

http://api.apkbus.com/reference/android/view/MotionEvent.html


MotiionEvent source code:

https://github.com/banxi1988/platform_frameworks_base/blob/master/core/java/android/view/MotionEvent.java


自己写了个小的debug demon帮助理解MotionEvent

package com.example.motionevent;



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.d("MotionEventTest","ACTION_POINTER_DOWN");
break;
case MotionEvent.ACTION_POINTER_UP:
Log.d("MotionEventTest","ACTION_POINTER_UP");
break;
case MotionEvent.ACTION_DOWN:
Log.d("MotionEventTest","ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d("MotionEventTest","ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.d("MotionEventTest","ACTION_UP");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("MotionEventTest","ACTION_CANCEL");
break;


default:
Log.d("liji3","default");
break;
}
 
return super.onTouchEvent(event);
}


}

猜你喜欢

转载自blog.csdn.net/lj19851227/article/details/45396421