欲优化,先度量。有啥办法可以精确地度量布局耗时?
读布局文件
以熟悉的setContentView()
为切入点,看看有没有突破口:
public class AppCompatActivity
@Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
}
点开setContentView()
源码,它的实现交给了一个代理,沿着调用链往下追查,最终的实现代码在AppCompatDelegateImpl
中:
class AppCompatDelegateImpl{
@Override
public void setContentView(int resId) {
ensureSubDecor();
//‘1.从顶层视图获得content视图’
ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content);
//‘2.移除所有子视图’
contentParent.removeAllViews();
//‘3.解析布局文件并填充到content视图中’
LayoutInflater.from(mContext).inflate(resId, contentParent);
mAppCompatWindowCallback.getWrapped().onContentChanged();
}
}
这三部中,最耗时操作应该是“解析布局文件”,点进去看看:
public abstract class LayoutInflater {
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
…
//‘获取布局文件解析器’
final XmlResourceParser parser = res.getLayout(resource);
try {
//‘填充布局’
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
}
先调用了getLayout()
获取了和布局文件对应的解析器,沿着调用链继续追查:
public class ResourcesImpl {
XmlResourceParser loadXmlResourceParser(@NonNull String file, @AnyRes int id, int assetCookie,@NonNull String type) throws NotFoundException {
if (id != 0) {
try {
synchronized (mCachedXmlBlocks) {
…
//‘通过AssetManager获取布局文件对象’
final XmlBlock block = mAssets.openXmlBlockAsset(assetCookie, file);
if (block != null) {
final int pos = (mLastCachedXmlBlockIndex + 1) % num;
mLastCachedXmlBlockIndex = pos;
final XmlBlock oldBlock = cachedXmlBlocks[pos];
if (oldBlock != null) {
oldBlock.close();
}
cachedXmlBlockCookies[pos] = assetCookie;
cachedXmlBlockFiles[pos] = file;
cachedXmlBlocks[pos] = block;
return block.newParser();
}
}
} catch (Exception e) {
…
}
}
…
}
}
沿着调用链,最终走到了ResourcesImpl.loadXmlResourceParser()
,它通过AssetManager.openXmlBlockAsset()
将 xml 布局文件转化成 Java 对象XmlBlock
:
public final class AssetManager implements AutoCloseable {
@NonNull XmlBlock openXmlBlockAsset(int cookie, @NonNull String fileName) throws IOException {
Preconditions.checkNotNull(fileName, ”fileName“);
synchronized (this) {
ensureOpenLocked();
//‘打开 xml 布局文件’
final long xmlBlock = nativeOpenXmlAsset(mObject, cookie, fileName);
if (xmlBlock == 0) {
//‘若打开失败则抛文件未找到异常’
throw new FileNotFoundException(“Asset XML file: ” + fileName);
}
final XmlBlock block = new XmlBlock(this, xmlBlock);
incRefsLocked(block.hashCode());
return block;
}
}
}
通过一个 native 方法,将布局文件读取到内存。走查到这里,有一件事可以确定,即 “解析 xml 布局文件前需要进行 IO 操作,将其读取至内存中”。
解析布局文件
读原码就好像“递归”,刚才通过不断地“递”,现在通过“归”回到那个关键方法:
public abstract class LayoutInflater {
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
…
//‘获取布局文件解析器’
final XmlResourceParser parser = res.getLayout(resource);
try {
//‘填充布局’
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
}
通过 IO 操作将布局文件读到内存后,调用了inflate()
:
public abstract class LayoutInflater {
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
…
try {
//‘根据布局文件的声明控件的标签构建 View’
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
//‘构建 View 对应的布局参数’
if (root != null) {
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
…
//‘将 View 填充到 View 树’
if (root != null && attachToRoot) {
root.addView(temp, params);
}
…
} catch (XmlPullParserException e) {
…
} finally {
…
}
return result;
}
}
这个方法解析布局文件并根据其中声明控件的标签构建 View实例,然后将其填充到 View 树中。解析布局文件的细节在createViewFromTag()
中:
public abstract class LayoutInflater {
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,boolean ignoreThemeAttr) {
…
try {
View view;
//‘通过Factory2.onCreateView()构建 View’
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
}
…
return view;
} catch (InflateException e) {
throw e;
}
…
}
}
onCreateView()
的具体实现在AppCompatDelegateImpl
中:
class AppCompatDelegateImpl{
@Override
public final View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
return createView(parent, name, context, attrs);
}
@Override
public View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs) {
if (mAppCompatViewInflater == null) {
TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);
String viewInflaterClassName =
a.getString(R.styleable.AppCompatTheme_viewInflaterClass);
if ((viewInflaterClassName == null){
…
} else {
try {
//‘通过反射获取AppCompatViewInflater实例’
Class<?> viewInflaterClass = Class.forName(viewInflaterClassName);
mAppCompatViewInflater =
(AppCompatViewInflater) viewInflaterClass.getDeclaredConstructor()
.newInstance();
} catch (Throwable t) {
…
}
}
}
boolean inheritContext = false;
if (IS_PRE_LOLLIPOP) {
inheritContext = (attrs instanceof XmlPullParser)
// If we have a XmlPullParser, we can detect where we are in the layout
? ((XmlPullParser) attrs).getDepth() > 1
// Otherwise we have to use the old heuristic
: shouldInheritContext((ViewParent) parent);
}
//‘通过createView()创建View实例’
return mAppCompatViewInflater.createView(parent, name, context, attrs, inheritContext,
IS_PRE_LOLLIPOP, /* Only read android:theme pre-L (L+ handles this anyway) /
true, / Read read app:theme as a fallback at all times for legacy reasons /
VectorEnabledTintResources.shouldBeUsed() / Only tint wrap the context if enabled */
);
}
}
AppCompatDelegateImpl
又把构建 View 委托给了 AppCompatViewInflater.createView()
:
final View createView(View parent, final String name, @NonNull Context context,
@NonNull AttributeSet attrs, boolean inheritContext,
boolean readAndroidTheme, boolean readAppTheme, boolean wrapContext) {
final Context originalContext = context;
…
View view = null;
//‘以布局文件中控件的名称分别创建对应控件实例’
switch (name) {
case “TextView”:
view = createTextView(context, attrs);
verifyNotNull(view, name);
break;
case “ImageView”:
view = createImageView(context, attrs);
verifyNotNull(view, name);
break;
case “Button”:
view = createButton(context, attrs);
verifyNotNull(view, name);
break;
case “EditText”:
view = createEditText(context, attrs);
verifyNotNull(view, name);
break;
case “Spinner”:
view = createSpinner(context, attrs);
verifyNotNull(view, name);
break;
case “ImageButton”:
view = createImageButton(context, attrs);
verifyNotNull(view, name);
break;
case “CheckBox”:
view = createCheckBox(context, attrs);
verifyNotNull(view, name);
break;
case “RadioButton”:
view = createRadioButton(context, attrs);
verifyNotNull(view, name);
break;
case “CheckedTextView”:
view = createCheckedTextView(context, attrs);
verifyNotNull(view, name);
break;
先自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以扫码领取!!!!
![](https://i-blog.csdnimg.cn/blog_migrate/0240993b6999fd2440b853e75576ca94.jpeg)
总结
其实客户端开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
然而Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可免费领取!
东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
[外链图片转存中…(img-WSorJn6g-1711282618594)]
[外链图片转存中…(img-cIkPENGs-1711282618594)]
然而Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可免费领取!