Android事件分发机制和触摸反馈

2018-09-30  本文已影响0人  Mr_丁先森

前言

首先,Android系统为什么会定义一套事件分发机制?
其实很简单,因为我们的布局里面View,ViewGroup是树状结构,一组事件顺序流过来,到底是View处理还是ViewGroup处理,都是在Android 事件分发机制下运行的,有了Android事件分发机制我们可以有效避免事件冲突,或者是可以很方便的自定义触摸反馈。


Android事件分发机制

机制下的核心类

public final class MotionEvent extends InputEvent implements Parcelable {

    /**
     * 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;

      //省略非核心代码
      ......
}

机制下的核心方法

  • dispatchTouchEvent()
  • onInterceptTouchEvent()
  • onTouchEvent()

机制的关键知识点

触摸反馈

上一篇 下一篇

猜你喜欢

热点阅读