android decorview

来源:互联网 发布:淘宝店铺怎么加入天猫 编辑:程序博客网 时间:2024/05/19 22:03

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, IVoiceInteractor voiceInteractor) {        attachBaseContext(context);        mFragments.attachActivity(this, mContainer, null);        /**  创建window  **/        mWindow = PolicyManager.makeNewWindow(this);        /**  设置window的回调对向为此activity **/        mWindow.setCallback(this);        mWindow.setOnWindowDismissedCallback(this);        mWindow.getLayoutInflater().setPrivateFactory(this);        if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {            mWindow.setSoftInputMode(info.softInputMode);        }        if (info.uiOptions != 0) {            mWindow.setUiOptions(info.uiOptions);        }        mUiThread = Thread.currentThread();        /**  得到ActivityThread **/        mMainThread = aThread;        mInstrumentation = instr;        mToken = token;        mIdent = ident;        mApplication = application;        mIntent = intent;        mComponent = intent.getComponent();        mActivityInfo = info;        mTitle = title;        mParent = parent;        mEmbeddedID = id;        mLastNonConfigurationInstances = lastNonConfigurationInstances;        if (voiceInteractor != null) {            if (lastNonConfigurationInstances != null) {                mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;            } else {                mVoiceInteractor = new VoiceInteractor(voiceInteractor, this, this,                        Looper.myLooper());            }        }        mWindow.setWindowManager(                (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),                mToken, mComponent.flattenToString(),                (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);        if (mParent != null) {            mWindow.setContainer(mParent.getWindow());        }        mWindowManager = mWindow.getWindowManager();        mCurrentConfig = config;    }
     r.window = r.activity.getWindow();     /** decorview是window子类, 通过window来获取 **/     View decor = r.window.getDecorView();      decor.setVisibility(View.INVISIBLE);     /** 获取WindowManager **/     ViewManager wm = a.getWindowManager();     WindowManager.LayoutParams l = r.window.getAttributes(); /** 给activity的mDecor赋值    **/      a.mDecor = decor;         if (a.mVisibleFromClient) {         a.mWindowAdded = true; /**这个调用会调用到WindowManagerGlobal的addView **/         wm.addView (decor, l);         }    WindowManagerGlobal的addView会    root = new ViewRootImpl(view.getContext(), display);           root.setView(view, wparams, panelParentView);    也就是说, ViewRootImpl的mView其实是DecorView.

activity - window - decorview - viewrootimp

每个activity有个window,window被windowmanager所管理.
每个window都有decorview.
每个window都有ViewRoot.

Activity
Activity 本身是由系统的ActivityManager进行管理的
Activity 是 Android 程序的一个呈现的载体,允许在其上面创建一个用户界面(window)

window
Activity创建后系统会调用其attach方法,将其添加到ActivityThread当中,在attach方法中创建了一个window对象。window对象是一个抽象类。要注意window对象创建时并木有创建 Decor对象。用户在Activity中调用setContentView,然后调用window的setContentView,这时会检查 DecorView是否存在,如果不存在则创建DecorView对象,然后把用户自己的View 添加到DecorView中。

在ActivityThread当中调用wm.addView(decor, l);把它加入到window manager proxy的mViews中,同时为这个decor view创建一个ViewRoot,ViewRoot负责协调decorview与windowmanager直接绘图、事件处理。说简单点就是 DecorView是客户端所有view的根,window manager proxy为这个decorview创建一个ViewRoot和Window manager service打交道

这里写图片描述

0 0
原创粉丝点击