android 加载过程,Android View (2) View的加载过程

View的加载过程

android 在加载视图的过程是通过在Activity的 setContentView(@LayoutRes int layoutResID) 来加载的,我们通过分析源码(在线源码链接)过程来理解。

setContentView(int layoutResID)源码分析

上篇我们说过WindowPhone类继承了Window类,Window类只是定义了一些方法和规范,所以我们直接看WindowPhone类中的方法。有三个重载方法,我们只分析一个,代码如下:

@Override

public void setContentView(int layoutResID) {

// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window

// decor, when theme attributes and the like are crystalized. Do not check the feature

// before this happens.

//判断是否第一次进入

if (mContentParent == null) {

//创建DecorView,并添加布局到mContentParent

installDecor();

//hasFeature(FEATURE_CONTENT_TRANSITIONS)是否设置了过场动画

} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {

//清空之前的View

mContentParent.removeAllViews();

}

if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {

//通过Scene执行过场动画(执行过程中会清空之前的View和添加新的布局)

final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,

getContext());

transitionTo(newScene);

} else {

//向mContentParent添加布局

mLayoutInflater.inflate(layoutResID, mContentParent);

}

mContentParent.requestApplyInsets();

final Callback cb = getCallback();

if (cb != null && !isDestroyed()) {

//回调通知表示完成界面加载

cb.onContentChanged();

}

// Whether the client has explicitly set the content view. If false and mContentParent is notnull, then the content parent was set due to window preservation.

//是否显式的设置了视图

mContentParentExplicitlySet = true;

}

接下来看installDecor() 代码如下:

private void installDecor() {

2640 mForceDecorInstall = false;

// 如果mDecor为空,则生成一个Decor,并设置其属性

2641 if (mDecor == null) {

// System process doesn't have application context and in that case we need to directly use the context we have.

// Otherwise we want the application context, so we don't cling to the activity.

//generateDecor()主要用于创建Decor且不依赖与Activity

2642 mDecor = generateDecor(-1);

//设置父View 与子View的聚焦关系

2643 mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

// 设置mDecor为整个Activity窗口的根节点,从此处可以看出窗口根节点为一个DecorView

2644 mDecor.setIsRootNamespace(true);

// 满足条件执行动画操作

2645 if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {

2646 mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);

2647 }

2648 } else {

2649 mDecor.setWindow(this);

2650 }

2651 if (mContentParent == null) {

//根据窗口的风格修饰,选择对应的修饰布局文件,并且将id为content的FrameLayout赋值给mContentParent

2652 mContentParent = generateLayout(mDecor);

2653

2654 // Set up decor part of UI to ignore fitsSystemWindows if appropriate.

2655 mDecor.makeOptionalFitsSystemWindows();

2656 //获取DecorView顶级布局,提供一些设置title,window主题等的方法

2657 final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(

2658 R.id.decor_content_parent);

2659

2660 if (decorContentParent != null) {

2661 //1. 将decorContentParent赋值给mDecorContentParent

//2. 设置窗口回调函数

//3.设置窗口的title、icon、logo等属性值

2699 } else {

2700 mTitleView = findViewById(R.id.title);

//这里有一个我们日常使用的ActionBar是否显示的判断,所以我们的设置要在setContentView方法之前

2701 if (mTitleView != null) {

2702 if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {

2703 final View titleContainer = findViewById(R.id.title_container);

2704 if (titleContainer != null) {

2705 titleContainer.setVisibility(View.GONE);

2706 } else {

2707 mTitleView.setVisibility(View.GONE);

2708 }

2709 mContentParent.setForeground(null);

2710 } else {

2711 mTitleView.setText(mTitle);

2712 }

2713 }

2714 }

2715

2716 if (mDecor.getBackground() == null && mBackgroundFallbackResource != 0) {

2717 mDecor.setBackgroundFallback(mBackgroundFallbackResource);

2718 }

2719 //....好多关于背景,动画等的判断,省略

2770 }

2771 }

2772 }

2773 }

接下来分析generateLayout(DecorView decor)方法的源码

//返回当前Activity的内容区域视图,即我们的布局文件显示区域mContentParent

protected ViewGroup generateLayout(DecorView decor) {

2309 // Apply data from current theme.

2310 //获取属性值

2311 TypedArray a = getWindowStyle();

2312 //根据Them来设置或修改各种属性,选定不同的资源文件等

.......

//设置title和background属性

2627 }

2628

2629 mDecor.finishChanging();

2630

2631 return contentParent;

2632 }

其实在geneateLayout中会根据我们在清单文件中设置的主题来进行加载相应的主题,布局等等。

总结

梳理setContentView()加载布局的过程:首先如果是首次调用的话,我们会创建DecorView对象作为Activity根视图,如果不是第一次调用,我们只需要移除之前的布局,继续用DecorView对象去作为根视图, 在加载布局过程中会通过设置的ThemStyle 设置一些属性值,然后通过findviewbyid的方式将跟布局文件添加到DecorView中,然后通过回调Activity的onContentChanged方法,通知布局加载完成

在加载View的过程中,系统是通过递归遍历一步步的进行解析加载的,所以我们在开发中,尽量不要嵌套太多层的布局,会影响到布局的加载效率,可以通过merge标签来减少嵌套层数。

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值