听 FaceBook 工程师讲 Custom ViewGroups
Android 提供了几个 ViewGroups 如 LinearLayout, RelativeLayout, FrameLayout 来固定 child Views 的位置。在这些普通的 ViewGroups 中有多种使用选择。 例如:LinearLayout 几乎支持 HTML Flexbox 的所有特性(除了包装)。
在 view 之间你可以选择是否显示分割线(dividers),并且基于最大的 child 测量所有的 children。RelativeLayout 是一种限制性的解决方案。这些 layouts 都已经足够好了,但是当你的 UI 非常复杂的时候它们还能很好的解决么?
ViewGroup with a ProfilePhoto, Title, Subtitle and Menu button.
上面的这种布局在 Facebook app 中是非常常见的。有头像、其它的 view 垂直摆在它的右侧、还有一个可选操作的 view 在最右边。这个布局可以通过使用 LinearLayout 嵌套或者一个 RelativeLayout 这样的普通 ViewGroup 实现。我们看一下当分别使用这两种布局的情况下在 measure 时会发生什么。
使用 LinearLayout 完成布局的示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProfilePhoto
android:layout_width="40dp"
android:layout_height="40dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Title
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Subtitle
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<Menu
android:layout_width="20dp"
android:layout_height="20dp"/>
</LinearLayout>
在 Nexus 5 设备上 measure 发生时的情况如下
> LinearLayout [horizontal] [w: 1080 exactly, h: 1557 exactly ]
> ProfilePhoto [w: 120 exactly, h: 120 exactly ]
> LinearLayout [vertical] [w: 0 unspecified, h: 0 unspecified]
> Title [w: 0 unspecified, h: 0 unspecified]
> Subtitle [w: 0 unspecified, h: 0 unspecified]
> Title [w: 222 exactly, h: 57 exactly ]
> Subtitle [w: 222 exactly, h: 57 exactly ]
> Menu [w: 60 exactly, h: 60 exactly ]
> LinearLayout [vertical] [w: 900 exactly, h: 1557 at_most ]
> Title [w: 900 exactly, h: 1557 at_most ]
> Subtitle [w: 900 exactly, h: 1500 at_most ]
ProfilePhoto 和 Menu 只被测量了一次,因为它们有明确的宽高值。垂直的 LinearLayout 被测量了两次。第一次的时候,父 LinearLayout 要求以 UNSPECIFIED spec 的方式来测量。导致了垂直的 LinearLayout 也以这种方式测量它的子 view.此时它在它们返回值的基础上以 EXACTLY spec 的方式测量它的子 view,但是它还没有结束。一旦在测量 ProfilePhoto 和 Menu 之后,父布局知道可用于垂直的 LinearLayout 的尺寸大小。以 AT_MOST height 对 Title 和 Subtitle 测量之后导致了第二次传值。显然,每一个 TextView (Title and Subtitle) 被测量 3 次。第二次传值创建或者废弃 Layouts,这些操作是昂贵的。如果想 ViewGroup 发挥更好的性能,首要的工作就是免去对 TextViews 的测量传值工作。
使用 RelativeLayout 效果会不会好一些?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProfilePhoto
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<Menu
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
<Title
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/profile_photo"
android:layout_toLeftOf="@id/menu"/>
<Subtitle
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toRightOf="@id/profile_photo"
android:layout_toLeftOf="@id/menu"/>
</RelativeLayout>
测量情况如下:
> RelativeLayout [w: 1080 exactly, h: 1557 exactly]
> Menu [w: 60 exactly, h: 1557 at_most]
> ProfilePhoto [w: 120 exactly, h: 1557 at_most]
> Title [w: 900 exactly, h: 1557 at_most]
> Subtitle [w: 900 exactly, h: 1557 at_most]
> Title [w: 900 exactly, h: 1557 at_most]
> Subtitle [w: 900 exactly, h: 1500 at_most]
> Menu [w: 60 exactly, h: 60 exactly]
> ProfilePhoto [w: 120 exactly, h: 120 exactly]
正如先前提到的,RelativeLayout 是通过 solving constraints(分解约束。译者认为就是一层一层的测量) 进行测量,上面的布局中 ProfilePhoto 和 Menu 没有依赖其它的参照物(siblings),因此它们先被测量(with an AT_MOST height).这时 Title(2 个约束) 和 Subtitle(3 个约束) 才会被测量。此时所有 view 明确了自己想要的尺寸大小。RelativeLayout 使用这些信息第二次传值给 Title, Subtitle, Menu 和 ProfilePhoto。再重复一遍,每个 view 被测量了两次,因此这种方案稍佳。如果你和上面的 LinearLayout 例子相比较一下,最后用于测量所有 leaf Views 所使用的 MeasureSpec 是相同的-因此最后的输出结果是一样的。
怎么样才能免去对子 view 的测量传值呢?自定义一个 ViewGroup 是不是会有帮助?让我们分析一下这个布局。Title 和 Subtitle 总是在 ProfilePhoto 的左侧在 Menu 按钮的右侧。如果我们手工解决这个问题,需要计算出 ProfilePhoto 和 Menu 按钮的尺寸,并且使用剩下的尺寸再来计算 Title 和 Subtitle。这时对每个 view 只进行一次测量传值。我们叫这种布局为 ProfilePhotoLayout。
public class ProfilePhotoLayout extends ViewGroup {
private ProfilePhoto mProfilePhoto;
private Menu mMenu;
private Title mTitle;
private Subtitle mSubtitle;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// 1. Setup initial constraints.
int widthConstraints = getPaddingLeft() + getPaddingRight();
int heightContraints = getPaddingTop() + getPaddingBottom();
int width = 0;
int height = 0;
// 2. Measure the ProfilePhoto
measureChildWithMargins(
mProfilePhoto,
widthMeasureSpec,
widthConstraints,
heightMeasureSpec,
heightConstraints);
// 3. Update the contraints.
widthConstraints += mProfilePhoto.getMeasuredWidth();
width += mProfilePhoto.getMeasuredWidth();
height = Math.max(mProfilePhoto.getMeasuredHeight(), height);
// 4. Measure the Menu.
measureChildWithMargins(
mMenu,
widthMeasureSpec,
widthConstraints,
heightMeasureSpec,
heightConstraints);
// 5. Update the constraints.
widthConstraints += mMenu.getMeasuredWidth();
width += mMenu.getMeasuredWidth();
height = Math.max(mMenu.getMeasuredHeight(), height);
// 6. Prepare the vertical MeasureSpec.
int verticalWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec) - widthConstraints,
MeasureSpec.getMode(widthMeasureSpec));
int verticalHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(heightMeasureSpec) - heightConstraints,
MeasureSpec.getMode(heightMeasureSpec));
// 7. Measure the Title.
measureChildWithMargins(
mTitle,
verticalWidthMeasureSpec,
0,
verticalHeightMeasureSpec,
0);
// 8. Measure the Subtitle.
measureChildWithMargins(
mSubtitle,
verticalWidthMeasureSpec,
0,
verticalHeightMeasureSpec,
mTitle.getMeasuredHeight());
// 9. Update the sizes.
width += Math.max(mTitle.getMeasuredWidth(), mSubtitle.getMeasuredWidth());
height = Math.max(mTitle.getMeasuredHeight() + mSubtitle.getMeasuredHeight(), height);
// 10. Set the dimension for this ViewGroup.
setMeasuredDimension(
resolveSize(width, widthMeasureSpec),
resolveSize(height, heightMeasureSpec));
}
@Override
protected void measureChildWithMargins(
View child,
int parentWidthMeasureSpec,
int widthUsed,
int parentHeightMeasureSpec,
int heightUsed) {
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidthMeasureSpec = getChildMeasureSpec(
parentWidthMeasureSpec,
widthUsed + lp.leftMargin + lp.rightMargin,
lp.width);
int childHeightMeasureSpec = getChildMeasureSpec(
parentHeightMeasureSpec,
heightUsed + lp.topMargin + lp.bottomMargin,
lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
}
我们来分析一下代码。我们从已知的约束条件开始 — 所有边的内边距,另外还需要考虑的约束是使用固定值的控件的高和宽。Android 提供了一个帮助方法-measureChildWithMargins() 用于测量 ViewGroup 内的子 view.然而它总是添加 padding 作为约束条件的一部分。因此我们复写这个方法自己来管理这些约束条件。
从测量 ProfilePhoto 开始,测量完成后更新一下 constraints。对 menu 按钮的测量亦是如此。 现在还剩下 Title 和 Subtitle 的宽度没有测量。Android 还提供了另外一个帮助方法-makeMeasureSpec(),用于构造 MeasureSpec,传入相应的 size 和 mode 返回一个 MeasureSpec。
接下来我们传入 Title 和 Subtitle 可用的 width 和 height 及相应的 MeasureSpecs 来测量 Title 和 Subtitle。最后更新一下 ViewGroup 的尺寸。在这一步可以明确每个 view 都只被测量一次。
> ProfilePhotoLayout [w: 1080 exactly, h: 1557 exactly]
> ProfilePhoto [w: 120 exactly, h: 120 exactly]
> Menu [w: 60 exactly, h: 60 exactly]
> Title [w: 900 exactly, h: 1557 at_most]
> Subtitle [w: 900 exactly, h: 1500 at_most]
性能上是不是提升了?Facebook app 中你看见的大多数布局都使用了这种布局,并且经证明确实提高了性能。我把没有提到的 onLayout() 方法留给读者作为练习。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论