Skip to content

Instantly share code, notes, and snippets.

@talenguyen
Created May 15, 2015 02:59
Show Gist options
  • Save talenguyen/20731c9816516abc2fa5 to your computer and use it in GitHub Desktop.
Save talenguyen/20731c9816516abc2fa5 to your computer and use it in GitHub Desktop.
Expert - CustomView group to get high performance
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment