美文网首页
一、Android触摸事件传递机制

一、Android触摸事件传递机制

作者: 锦文豪武 | 来源:发表于2018-09-21 16:30 被阅读0次

    A、触摸事件的类型

    注:在一次屏幕触摸事件中ACTION_DOWN 和 ACTION_UP是必需的。ACTION_MOVE只是移动时候触发


    微信图片_20180921154114.png
    /**
     * 屏幕触摸事件
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            Log.d("dispatchTouchEvent","ACTION_DOWN");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("dispatchTouchEvent","ACTION_MOVE");
            break;
        case MotionEvent.ACTION_UP:
            Log.d("dispatchTouchEvent","ACTION_UP");
            break;
    }
    return super.dispatchTouchEvent(ev);
    }
    

    B、事件传递的三个阶段

    注:分发、拦截、消费


    微信图片_20180921154443.png
    /**
     * 触摸事件,第一阶段事件分发,方法返回true 表示事件被当前视图消费掉,不在继续分发事件 ,返回super.dispatchTouchEvent(ev)表示继续分发该事件
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    switch (ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            Log.d("dispatchTouchEvent","ACTION_DOWN");
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("dispatchTouchEvent","ACTION_MOVE");
            break;
        case MotionEvent.ACTION_UP:
            Log.d("dispatchTouchEvent","ACTION_UP");
            break;
    }
    return super.dispatchTouchEvent(ev);
    }
    
    微信图片_20180921155417.png
    /**
     * Created by chaohao.zhao on 2018/8/24.
     */
    
    public class CustomTextView extends LinearLayout{
    public CustomTextView(Context context) {
        super(context);
    }
    /***
     * 只在ViewGroup及其子类中才存在,在Veiw 和Activity中是不存在的
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d("onInterceptTouchEvent",""+ev.getAction());
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("onInterceptTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("onInterceptTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("onInterceptTouchEvent","ACTION_UP");
                break;
        }
        /**
         * 返回super.onInterceptTouchEvent(ev) 或者 false 表示不对事件进行拦截。需要继续传给子shitu
         * */
        return true;
    }
    }
    
    微信图片_20180921155516.png

    在Android 系统中,拥有事件传递处理能力的类有三种

    微信图片_20180921155606.png

    C、View事件的传递机制

    注:ViewGroup是View的子类,这里说的View指的是ViewGroup外的View控件,例:Botton,CheckBox,TextView等。View 控件本身已经是最小的单位,不能在作为其他的view容器

    (1)下面是举例:有两个

    a、View:

    public class CustomTextView extends LinearLayout{
    public CustomTextView(Context context) {
        super(context);
    }
    
    public CustomTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    /****
     * 第三步 执行
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("onTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("onTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("onTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("onTouchEvent","ACTION_CANCEL");
                break;
                default:
                    break;
        }
        /***如果返回值设置为true,那么就会再次拦截,不会传递下层view 或Viewroup*/
        return super.onTouchEvent(event);
    }
    
    /***
     * 第二个分发到这里然后进行分发
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("dispatchTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("dispatchTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("dispatchTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("dispatchTouchEvent","ACTION_CANCEL");
                break;
            default:
                break;
        }
        /***如果返回值设置为true,那么就会再次拦截,不会传递下层view 或Viewroup*/
        return super.dispatchTouchEvent(ev);
    }
    }
    

    b、MainActivity:

    public class MainActivity extends AppCompatActivity implements View.OnTouchListener , View.OnClickListener{
    TextView txt;
    CustomTextView custom_text;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        custom_text=findViewById(R.id.custom_text);
        txt=findViewById(R.id.txt);
        custom_text.setOnClickListener(this);//设置点击处理
        custom_text.setOnTouchListener(this);//设置触摸处理
    }
    
    /**
     * 第一个响应,进行事件分发
     * 触摸事件,第一阶段事件分发,方法返回true 表示事件被当前视图消费掉,不在继续分发事件 ,返回super.dispatchTouchEvent(ev)表示继续分发该事件
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("Main_dispatchTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("Main_dispatchTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("Main_dispatchTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("Main_dispatchTouchEvent","ACTION_CANCEL");
                break;
                default:
                    break;
        }
        /**如果返回值设置为true,那么就会再次拦截,不会传递下层view 或Viewroup*/
        return super.dispatchTouchEvent(ev);
    }
    
    /***
     * 消费,第四步
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("Main_onTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("Main_onTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("Main_onTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("Main_onTouchEvent","ACTION_CANCEL");
                break;
                default:
                    break;
        }
        return super.onTouchEvent(event);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.custom_text:
                Log.d("Main_onClick","onClick");
                break;
        }
    
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()){
            case R.id.custom_text:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        Log.d("Main_onTouch","ACTION_DOWN");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d("Main_onTouch","ACTION_MOVE");
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d("Main_onTouch","ACTION_UP");
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        Log.d("Main_onTouch","ACTION_CANCEL");
                        break;
                    default:
                        break;
                }
                break;
        }
        return false;
    }
    }
    

    通过测试运行发现顺序如下:


    图片1.png

    注:其中返回值若为true或者false,那么就不会向下分发

    View的执行顺序是先执行onTouch方法,最后才执行onClick方法,如果onTouch 返回为true,则事件不会继续传递,最后也不会调用onclick方法,如果返回false,则事件继续传递

    D、ViewGroup的事件传递机制

    ViewGroup是作为View控件的容器存在的,Android系统默认提供了一系列ViewGroup子类,常见的有LinearLayout,RelativeLayout、FramLayout、ListView、ScrollView等,拥有的方法:


    微信图片_20180921160308.png

    下面是一个例子:这是一个ViewGroup

    public class CustomRelativeLayout extends RelativeLayout{
    public CustomRelativeLayout(Context context) {
        super(context);
    }
    
    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("ReladispatchTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("ReladispatchTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("ReladispatchTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("ReladispatchTouchEvent","ACTION_CANCEL");
                break;
            default:
                break;
        }
        /***如果返回值设置为true,那么就会再次拦截,不会传递下层view 或Viewroup*/
        return super.dispatchTouchEvent(ev);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("RelaonTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("RelaonTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("RelaonTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("RelaonTouchEvent","ACTION_CANCEL");
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("ReonInterceptTouchEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("ReonInterceptTouchEvent","ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d("ReonInterceptTouchEvent","ACTION_UP");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.d("ReonInterceptTouchEvent","ACTION_CANCEL");
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
    }
    

    运行后的一个顺序:触摸事件顺序是由Activity 到ViewGroup ,再由ViewGroup递归传递给其他子类
    ViewGroup 通过onInterceptTouchEvent方法进行拦截,如果该方法返回为true,则事件不会继续传给子view,否则会继续传给子view.
    在子view中对事件进行消费后,ViewGroup将接收不到任何事件

    图片2.png

    相关文章

      网友评论

          本文标题:一、Android触摸事件传递机制

          本文链接:https://www.haomeiwen.com/subject/uizanftx.html