FrameLayout与LinearLayout性能对比
首先,我们来看FrameLayout的性能表现:
LinearLayout性能数据
Measure: 2.058ms
Layout: 0.296ms
Draw: 3.857ms
FrameLayout性能数据
Measure: 1.334ms
Layout: 0.213ms
Draw: 3.680ms
从上述数据可以看出,使用LinearLayout时,仅嵌套一个LinearLayout,在onMeasure阶段的时间几乎是FrameLayout的两倍,而在layout和draw阶段两者相差不大。考虑到测量误差,可以认为两者在这些阶段的表现相当。
接下来,我们看一下FrameLayout的源码,了解其内部机制:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
mMatchParentChildren.clear();
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
for (int i = 0; i View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT || lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
Drawable drawable = getForeground();
if (drawable != null) {
maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
}
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec, childState < count = mMatchParentChildren.size();
if (count > 1) {
for (int i = 0; i View child = mMatchParentChildren.get(i);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidthMeasureSpec;
int childHeightMeasureSpec;
if (lp.width == LayoutParams.MATCH_PARENT) {
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth() -
getPaddingLeftWithForeground() -
getPaddingRightWithForeground() -
lp.leftMargin - lp.rightMargin,
MeasureSpec.EXACTLY);
} else {
childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
getPaddingLeftWithForeground() +
getPaddingRightWithForeground() +
lp.leftMargin + lp.rightMargin,
lp.width);
}
if (lp.height == LayoutParams.MATCH_PARENT) {
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight() -
getPaddingTopWithForeground() -
getPaddingBottomWithForeground() -
lp.topMargin - lp.bottomMargin,
MeasureSpec.EXACTLY);
} else {
childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
getPaddingTopWithForeground() +
getPaddingBottomWithForeground() +
lp.topMargin + lp.bottomMargin,
lp.height);
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
}
当我们增加一层嵌套时,onMeasure的时间几乎增加了一倍。原因在于LinearLayout在某一方向上的onMeasure过程中,如果发现子View中还有LinearLayout,会触发额外的遍历和测量操作。例如,当二级LinearLayout的父类是Match_parent时,会导致再次遍历,从而增加时间消耗。
结论
1. RelativeLayout会使子View调用两次onMeasure,而LinearLayout在使用weight属性时也会调用子View两次onMeasure。
2. 如果RelativeLayout的子View高度与其不同,会导致效率问题,特别是当子View非常复杂时,这一问题会更加严重。建议尽可能使用padding代替margin以提高性能。
3. 在不影响层级深度的情况下,优先使用LinearLayout和FrameLayout而不是RelativeLayout。
4. 能用两个LinearLayout实现的布局,尽量用一个RelativeLayout,这样可以减少时间消耗。另外,谨慎使用LinearLayout的layout_weight属性,因为它会增加一倍的测量时间。由于使用layout_weight时,大多数时间是不一致的,这会降低测量速度。总之,减少层级结构是提高性能的关键,可以使用viewStub、include等技巧来优化布局。