Android View 事件分发机制 源代码解析 (上)

一直想写事件分发机制的文章,无论咋样,也得自己研究下事件分发的源代码。写出心得~

首先我们先写个简单的样例来測试View的事件转发的流程~

1、案例

为了更好的研究View的事件转发,我们自定以一个MyButton继承Button,然后把跟事件传播有关的方法进行复写。然后加入上日志~

MyButton

package com.example.zhy_event03;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;

public class MyButton extends Button
{
	private static final String TAG = MyButton.class.getSimpleName();

	public MyButton(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		int action = event.getAction();

		switch (action)
		{
		case MotionEvent.ACTION_DOWN:
			Log.e(TAG, "onTouchEvent ACTION_DOWN");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.e(TAG, "onTouchEvent ACTION_MOVE");
			break;
		case MotionEvent.ACTION_UP:
			Log.e(TAG, "onTouchEvent ACTION_UP");
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event)
	{
		int action = event.getAction();

		switch (action)
		{
		case MotionEvent.ACTION_DOWN:
			Log.e(TAG, "dispatchTouchEvent ACTION_DOWN");
			break;
		case MotionEvent.ACTION_MOVE:
			Log.e(TAG, "dispatchTouchEvent ACTION_MOVE");
			break;
		case MotionEvent.ACTION_UP:
			Log.e(TAG, "dispatchTouchEvent ACTION_UP");
			break;

		default:
			break;
		}
		return super.dispatchTouchEvent(event);
	}

	
}

在onTouchEvent和dispatchTouchEvent中打印了日志~

然后把我们自己定义的按钮加到主布局文件里;

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.example.zhy_event03.MyButton
        android:id="@+id/id_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click me" />

</LinearLayout>

最后看一眼MainActivity的代码

package com.example.zhy_event03;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class MainActivity extends Activity
{
	protected static final String TAG = "MyButton";
	private Button mButton ;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mButton = (Button) findViewById(R.id.id_btn);
		mButton.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				int action = event.getAction();

				switch (action)
				{
				case MotionEvent.ACTION_DOWN:
					Log.e(TAG, "onTouch ACTION_DOWN");
					break;
				case MotionEvent.ACTION_MOVE:
					Log.e(TAG, "onTouch ACTION_MOVE");
					break;
				case MotionEvent.ACTION_UP:
					Log.e(TAG, "onTouch ACTION_UP");
					break;
				default:
					break;
				}
				
				return false;
			}
		});
	}

	
}

在MainActivity中,我们还给MyButton设置了OnTouchListener这个监听~

好了。跟View事件相关一般就这三个地方了,一个onTouchEvent,一个dispatchTouchEvent。一个setOnTouchListener;

以下我们运行,然后点击按钮,查看日志输出:

08-31 06:09:39.030: E/MyButton(879): dispatchTouchEvent ACTION_DOWN
08-31 06:09:39.030: E/MyButton(879): onTouch ACTION_DOWN
08-31 06:09:39.049: E/MyButton(879): onTouchEvent ACTION_DOWN
08-31 06:09:39.138: E/MyButton(879): dispatchTouchEvent ACTION_MOVE
08-31 06:09:39.138: E/MyButton(879): onTouch ACTION_MOVE
08-31 06:09:39.147: E/MyButton(879): onTouchEvent ACTION_MOVE
08-31 06:09:39.232: E/MyButton(879): dispatchTouchEvent ACTION_UP
08-31 06:09:39.248: E/MyButton(879): onTouch ACTION_UP
08-31 06:09:39.248: E/MyButton(879): onTouchEvent ACTION_UP

我有意点击的时候蹭了一下,不然不会触发MOVE,手抖可能会打印一堆MOVE的日志~~~

好了。能够看到。无论是DOWN,MOVE,UP都会依照以下的顺序运行:

1、dispatchTouchEvent

2、 setOnTouchListener的onTouch

3、onTouchEvent

以下就尾随日志的脚步開始源代码的探索~

2、dispatchTouchEvent

首先进入View的dispatchTouchEvent

/**
     * Pass the touch screen motion event down to the target view, or this
     * view if it is the target.
     *
     * @param event The motion event to be dispatched.
     * @return True if the event was handled by the view, false otherwise.
     */
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (!onFilterTouchEventForSecurity(event)) {
            return false;
        }

        if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
                mOnTouchListener.onTouch(this, event)) {
            return true;
        }
        return onTouchEvent(event);
    }

直接看13行:首先推断mOnTouchListener不为null。而且view是enable的状态,然后 mOnTouchListener.onTouch(this, event)返回true。这三个条件假设都满足,直接return true ; 也就是以下的onTouchEvent(event)不会被运行了;

那么mOnTouchListener是和方神圣,我们来看看:

   /**
     * Register a callback to be invoked when a touch event is sent to this view.
     * @param l the touch listener to attach to this view
     */
    public void setOnTouchListener(OnTouchListener l) {
        mOnTouchListener = l;
    }
事实上就是我们在Activity中设置的setOnTouchListener。

也就是说:假设我们设置了setOnTouchListener,而且return true,那么View自己的onTouchEvent就不会被运行了。当然了,本例我们return false。我们还得往下探索 ;

已经解决一个常见的问题:View的onTouchListener和onTouchEvent的调用关系,相信大家应该已经明确了~let's go;继续往下。

 3、View的onTouchEvent:

接下来是View的onTouchEvent:

/**
     * Implement this method to handle touch screen motion events.
     *
     * @param event The motion event.
     * @return True if the event was handled, false otherwise.
     */
    public boolean onTouchEvent(MotionEvent event) {
        final int viewFlags = mViewFlags;

        if ((viewFlags & ENABLED_MASK) == DISABLED) {
            // A disabled view that is clickable still consumes the touch
            // events, it just doesn't respond to them.
            return (((viewFlags & CLICKABLE) == CLICKABLE ||
                    (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
        }

        if (mTouchDelegate != null) {
            if (mTouchDelegate.onTouchEvent(event)) {
                return true;
            }
        }

        if (((viewFlags & CLICKABLE) == CLICKABLE ||
                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
                    if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
                        // take focus if we don't have it already and we should in
                        // touch mode.
                        boolean focusTaken = false;
                        if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
                            focusTaken = requestFocus();
                        }

                        if (!mHasPerformedLongPress) {
                            // This is a tap, so remove the longpress check
                            removeLongPressCallback();

                            // Only perform take click actions if we were in the pressed state
                            if (!focusTaken) {
                                // Use a Runnable and post this rather than calling
                                // performClick directly. This lets other visual state
                                // of the view update before click actions start.
                                if (mPerformClick == null) {
                                    mPerformClick = new PerformClick();
                                }
                                if (!post(mPerformClick)) {
                                    performClick();
                                }
                            }
                        }

                        if (mUnsetPressedState == null) {
                            mUnsetPressedState = new UnsetPressedState();
                        }

                        if (prepressed) {
                            mPrivateFlags |= PRESSED;
                            refreshDrawableState();
                            postDelayed(mUnsetPressedState,
                                    ViewConfiguration.getPressedStateDuration());
                        } else if (!post(mUnsetPressedState)) {
                            // If the post failed, unpress right now
                            mUnsetPressedState.run();
                        }
                        removeTapCallback();
                    }
                    break;

                case MotionEvent.ACTION_DOWN:
                    if (mPendingCheckForTap == null) {
                        mPendingCheckForTap = new CheckForTap();
                    }
                    mPrivateFlags |= PREPRESSED;
                    mHasPerformedLongPress = false;
                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                    break;

                case MotionEvent.ACTION_CANCEL:
                    mPrivateFlags &= ~PRESSED;
                    refreshDrawableState();
                    removeTapCallback();
                    break;

                case MotionEvent.ACTION_MOVE:
                    final int x = (int) event.getX();
                    final int y = (int) event.getY();

                    // Be lenient about moving outside of buttons
                    int slop = mTouchSlop;
                    if ((x < 0 - slop) || (x >= getWidth() + slop) ||
                            (y < 0 - slop) || (y >= getHeight() + slop)) {
                        // Outside button
                        removeTapCallback();
                        if ((mPrivateFlags & PRESSED) != 0) {
                            // Remove any future long press/tap checks
                            removeLongPressCallback();

                            // Need to switch from pressed to not pressed
                            mPrivateFlags &= ~PRESSED;
                            refreshDrawableState();
                        }
                    }
                    break;
            }
            return true;
        }

        return false;
    }

代码还是比較长的,

10-15行,假设当前View是Disabled状态且是可点击则会消费掉事件(return true);能够忽略,不是我们的重点;

17-21行。假设设置了mTouchDelegate,则会将事件交给代理者处理,直接return true,假设大家希望自己的View添加它的touch范围,能够尝试使用TouchDelegate,这里也不是重点,能够忽略。

接下来到我们的重点了:

23行的推断:假设我们的View能够点击或者能够长按,则,注意IF的范围。最终一定return true ;

 if (((viewFlags & CLICKABLE) == CLICKABLE ||
                (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
           //...
            return true;
        }

接下来就是   switch (event.getAction())了。推断事件类型,DOWN,MOVE,UP等;

我们依照样例运行的顺序,先看  case MotionEvent.ACTION_DOWN (71-78行):

1、MotionEvent.ACTION_DOWN

75行:给mPrivateFlags设置一个PREPRESSED的标识

76行:设置mHasPerformedLongPress=false;表示长按事件还未触发;

77行:发送一个延迟为ViewConfiguration.getTapTimeout()的延迟消息,到达延时时间后会运行CheckForTap()里面的run方法:

1、ViewConfiguration.getTapTimeout()为115毫秒;

2、CheckForTap

  private final class CheckForTap implements Runnable {
        public void run() {
            mPrivateFlags &= ~PREPRESSED;
            mPrivateFlags |= PRESSED;
            refreshDrawableState();
            if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
                postCheckForLongClick(ViewConfiguration.getTapTimeout());
            }
        }
    }

在run方法里面取消mPrivateFlags的PREPRESSED。然后设置PRESSED标识。刷新背景。假设View支持长按事件。则再发一个延时消息。检測长按;

 private void postCheckForLongClick(int delayOffset) {
        mHasPerformedLongPress = false;

        if (mPendingCheckForLongPress == null) {
            mPendingCheckForLongPress = new CheckForLongPress();
        }
        mPendingCheckForLongPress.rememberWindowAttachCount();
        postDelayed(mPendingCheckForLongPress,
                ViewConfiguration.getLongPressTimeout() - delayOffset);
    }

class CheckForLongPress implements Runnable {

        private int mOriginalWindowAttachCount;

        public void run() {
            if (isPressed() && (mParent != null)
                    && mOriginalWindowAttachCount == mWindowAttachCount) {
                if (performLongClick()) {
                    mHasPerformedLongPress = true;
                }
            }
        }

能够看到。当用户按下,首先会设置标识为PREPRESSED

假设115后,没有抬起,会将View的标识设置为PRESSED且去掉PREPRESSED标识,然后发出一个检測长按的延迟任务,延时为:ViewConfiguration.getLongPressTimeout() - delayOffset(500ms -115ms),这个115ms刚好时检測额PREPRESSED时间;也就是用户从DOWN触发開始算起,假设500ms内没有抬起则觉得触发了长按事件:

1、假设此时设置了长按的回调,则运行长按时的回调。且假设长按的回调返回true;才把mHasPerformedLongPress置为ture;

2、否则,假设没有设置长按回调或者长按回调返回的是false;则mHasPerformedLongPress依旧是false;

好了DOWN就分析完毕了;大家回个神,以下回到VIEW的onTouchEvent中的ACTION_MOVE:

2、MotionEvent.ACTION_MOVE

86到105行:

87-88行:拿到当前触摸的x,y坐标;

91行推断当然触摸点有没有移出我们的View,假设移出了:

1、运行removeTapCallback(); 

2、然后推断是否包括PRESSED标识,假设包括。移除长按的检查:removeLongPressCallback();

3、最后把mPrivateFlags中PRESSED标识去除,刷新背景。

 private void removeTapCallback() {
        if (mPendingCheckForTap != null) {
            mPrivateFlags &= ~PREPRESSED;
            removeCallbacks(mPendingCheckForTap);
        }
    }
这个是移除,DOWN触发时设置的PREPRESSED的检測。即当前触发时机在DOWN触发不到115ms时,你就已经移出控件外了;

假设115ms后,你才移出控件外,则你的当前mPrivateFlags一定为PRESSED且发送了长按的检測;

就会走上面的2和3;首先移除removeLongPressCallback()
 private void removeLongPressCallback() {
        if (mPendingCheckForLongPress != null) {
          removeCallbacks(mPendingCheckForLongPress);
        }
    }

然后mPrivateFlags中PRESSED标识去除,刷新背景。

好了,MOVE我们也分析完毕了,总结一下:仅仅要用户移出了我们的控件:则将mPrivateFlags取出PRESSED标识,且移除全部在DOWN中设置的检測,长按等;

以下再回个神,回到View的onTouchEvent的ACTION_UP:

3、MotionEvent.ACTION_UP

26到69行:

27行:推断mPrivateFlags是否包括PREPRESSED

28行:假设包括PRESSED或者PREPRESSED则进入运行体,也就是无论是115ms内或者之后抬起都会进入运行体。

36行:假设mHasPerformedLongPress没有被运行,进入IF

38行:removeLongPressCallback()。移除长按的检測

45-50行:假设mPerformClick假设mPerformClick为null,初始化一个实例,然后马上通过handler加入到消息队列尾部,假设加入失败则直接运行 performClick();加入成功,在mPerformClick的run方法中就是运行performClick();

最终运行了我们的click事件了,以下看一下performClick()方法:

 public boolean performClick() {
        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);

        if (mOnClickListener != null) {
            playSoundEffect(SoundEffectConstants.CLICK);
            mOnClickListener.onClick(this);
            return true;
        }

        return false;
    }

if (mOnClickListener != null) {    
            mOnClickListener.onClick(this);
            return true;
        }

久违了~我们的mOnClickListener 。

别激动,还没结束,回到ACTION_UP,

58行:假设prepressed为true,进入IF体:

为mPrivateFlags设置表示为PRESSED,刷新背景,125毫秒后运行mUnsetPressedState

否则:mUnsetPressedState.run();马上运行;也就是无论咋样,最后mUnsetPressedState.run()都会运行;

看看这个UnsetPressedState主要干什么:

  private final class UnsetPressedState implements Runnable {
        public void run() {
            setPressed(false);
        }
    }

 public void setPressed(boolean pressed) {
        if (pressed) {
            mPrivateFlags |= PRESSED;
        } else {
            mPrivateFlags &= ~PRESSED;
        }
        refreshDrawableState();
        dispatchSetPressed(pressed);
    }

把我们的mPrivateFlags中的PRESSED取消,然后刷新背景。把setPress转发下去。

ACTION_UP的最后一行:removeTapCallback(),假设mPendingCheckForTap不为null,移除;

4、总结

好了。代码跨度还是相当大的,以下须要总结下:

1、整个View的事件转发流程是:

View.dispatchEvent->View.setOnTouchListener->View.onTouchEvent

在dispatchTouchEvent中会进行OnTouchListener的推断,假设OnTouchListener不为null且返回true。则表示事件被消费,onTouchEvent不会被运行;否则运行onTouchEvent。

2、onTouchEvent中的DOWN,MOVE,UP

DOWN时:

a、首先设置标志为PREPRESSED,设置mHasPerformedLongPress=false ;然后发出一个115ms后的mPendingCheckForTap;

b、假设115ms内没有触发UP,则将标志置为PRESSED,清除PREPRESSED标志,同一时候发出一个延时为500-115ms的,检測长按任务消息;

c、假设500ms内(从DOWN触发開始算)。则会触发LongClickListener:

此时假设LongClickListener不为null,则会运行回调。同一时候假设LongClickListener.onClick返回true,才把mHasPerformedLongPress设置为true;否则mHasPerformedLongPress依旧为false;

MOVE时:

主要就是检測用户是否划出控件。假设划出了:

115ms内,直接移除mPendingCheckForTap;

115ms后,则将标志中的PRESSED去除,同一时候移除长按的检查:removeLongPressCallback();

UP时:

a、假设115ms内。触发UP,此时标志为PREPRESSED,则运行UnsetPressedState。setPressed(false);会把setPress转发下去,能够在View中复写dispatchSetPressed方法接收;

b、假设是115ms-500ms间,即长按还未发生,则首先移除长按检測,运行onClick回调;

c、假设是500ms以后。那么有两种情况:

i.设置了onLongClickListener,且onLongClickListener.onClick返回true,则点击事件OnClick事件无法触发;

ii.没有设置onLongClickListener或者onLongClickListener.onClick返回false。点击事件OnClick事件依旧能够触发;

d、最后运行mUnsetPressedState.run()。将setPressed传递下去,然后将PRESSED标识去除;


最后问个问题,然后再运行个样例结束:

1、setOnLongClickListener和setOnClickListener是否仅仅能运行一个

不是的,仅仅要setOnLongClickListener中的onClick返回false,则两个都会运行;返回true则会屏幕setOnClickListener

最后我们给MyButton同一时候设置setOnClickListener和setOnLongClickListener,运行看看:

package com.example.zhy_event03;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
	protected static final String TAG = "MyButton";
	private Button mButton ;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mButton = (Button) findViewById(R.id.id_btn);
		mButton.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				int action = event.getAction();

				switch (action)
				{
				case MotionEvent.ACTION_DOWN:
					Log.e(TAG, "onTouch ACTION_DOWN");
					break;
				case MotionEvent.ACTION_MOVE:
					Log.e(TAG, "onTouch ACTION_MOVE");
					break;
				case MotionEvent.ACTION_UP:
					Log.e(TAG, "onTouch ACTION_UP");
					break;
				default:
					break;
				}
				
				return false;
			}
		});
		mButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Toast.makeText(getApplicationContext(), "onclick",Toast.LENGTH_SHORT).show();
			}
		});
		
		mButton.setOnLongClickListener(new OnLongClickListener()
		{
			@Override
			public boolean onLongClick(View v)
			{
				Toast.makeText(getApplicationContext(), "setOnLongClickListener",Toast.LENGTH_SHORT).show();
				return false;
			}
		});
	}

	
}
效果图:


能够看到LongClickListener已经ClickListener都触发了~


最后。本篇博文完毕了对View的事件分发机制的整个流程的说明,而且对源代码进行了分析;

当然了。View结束,肯定到我们的ViewGroup了。请点击:Android ViewGroup事件分发机制



posted @ 2017-06-13 16:37  lytwajue  阅读(217)  评论(0编辑  收藏  举报