View系列-事件分发

1.Activity.dispatchTouchEvent 

//frameworks\base\core\java\android\app\Activity.java
public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {

    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        if (getWindow().superDispatchTouchEvent(ev)) { //通过PhoneWindow进行分发事件
            return true;
        }
        return onTouchEvent(ev);
    }
}

2.Activity.getWindow()

//frameworks\base\core\java\android\app\Activity.java

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {
    
    private Window mWindow; //Activity的一个对象,在attach方法里初始化

    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
        mWindow = new PhoneWindow(this);
    }
    
    public Window getWindow() {
        return mWindow;
    }
}

3.PhoneWindow.superDispatchTouchEvent

继续调用DecorView的superDispatchTouchEvent 

//frameworks\base\core\java\com\android\internal\policy\PhoneWindow.java
public class PhoneWindow extends Window implements MenuBuilder.Callback {
    
    // This is the top-level view of the window, containing the window decor.
    private DecorView mDecor;

    @Override
    public boolean superDispatchTouchEvent(MotionEvent event) {
        return mDecor.superDispatchTouchEvent(event);
    }
}

4.DecorView.superDispatchTouchEvent 

//frameworks\base\core\java\com\android\internal\policy\PhoneWindow.java

public class PhoneWindow extends Window implements MenuBuilder.Callback {    

    private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {

        public boolean superDispatchTouchEvent(MotionEvent event) {
            return super.dispatchTouchEvent(event);
        }
    }
}

5.ViewGroup.dispatchTouchEvent

//frameworks\base\core\java\android\view\ViewGroup.java

public abstract class ViewGroup extends View implements ViewParent, ViewManager {

    public boolean dispatchTouchEvent(MotionEvent ev) {
        //省略代码
    }
}

6.Activity、PhoneWindow、DecorView

6.1.Activity和PhoneWindow

PhoneWindow是Activity的一个对象

//frameworks\base\core\java\android\app\Activity.java

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {
    
    private Window mWindow; //Activity的一个对象,在attach方法里初始化

    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
        mWindow = new PhoneWindow(this);
    }
    
    public Window getWindow() {
        return mWindow;
    }
}
6.2.PhoneWindow和DecorView

DecorView是PhoneWindow的一个对象

DecorView继承自FrameLayout 

public class PhoneWindow extends Window implements MenuBuilder.Callback {
    // This is the top-level view of the window, containing the window decor.
    private DecorView mDecor; //继承自FrameLayout

    @Override
    public final View getDecorView() {
        if (mDecor == null) {
            installDecor();
        }
        return mDecor;
    }

    private void installDecor() {
        if (mDecor == null) {
            mDecor = generateDecor();
            //省略代码
        }
        //省略代码
    }

    protected DecorView generateDecor() {
        return new DecorView(getContext(), -1);
    }
    
    private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
        
    }
    
}

你可能感兴趣的:(Android,android)