控件阅读的一般入口为onMeasure方法以及onLayout方法。本次源码阅读,主要看看ConstraintLayout如何进行布局约束的。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (OPTIMIZE_HEIGHT_CHANGE
&& mOnMeasureWidthMeasureSpec == widthMeasureSpec
&& MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY
&& MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST
&& MeasureSpec.getMode(mOnMeasureHeightMeasureSpec) == MeasureSpec.AT_MOST) {
int newSize = MeasureSpec.getSize(heightMeasureSpec);
if (DEBUG) {
System.out.println("### COMPATIBLE REQ " + newSize + " >= ? " + mLayoutWidget.getHeight());
}
if (newSize >= mLayoutWidget.getHeight() && !mLayoutWidget.isHeightMeasuredTooSmall()) {
mOnMeasureWidthMeasureSpec = widthMeasureSpec;
mOnMeasureHeightMeasureSpec = heightMeasureSpec;
resolveMeasuredDimension(widthMeasureSpec, heightMeasureSpec, mLayoutWidget.getWidth(), mLayoutWidget.getHeight(),
mLayoutWidget.isWidthMeasuredTooSmall(), mLayoutWidget.isHeightMeasuredTooSmall());
return;
}
}
}
if (mDirtyHierarchy) {
mDirtyHierarchy = false;
if (updateHierarchy()) {
mLayoutWidget.updateHierarchy();
}
}
resolveSystem(mLayoutWidget, mOptimizationLevel, widthMeasureSpec, heightMeasureSpec);
resolveMeasuredDimension(widthMeasureSpec, heightMeasureSpec, mLayoutWidget.getWidth(), mLayoutWidget.getHeight(),
mLayoutWidget.isWidthMeasuredTooSmall(), mLayoutWidget.isHeightMeasuredTooSmall());
}
核心方法为 updateHierarchy() 方法,看下源码
private boolean updateHierarchy() {
final int count = getChildCount();
boolean recompute = false;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.isLayoutRequested()) {
recompute = true;
break;
}
}
if (recompute) {
setChildrenConstraints();
}
return recompute;
}
该方法的核心方法为 setChildrenConstraints()
private void setChildrenConstraints() {
...
if (USE_CONSTRAINTS_HELPER && mConstraintSetId != -1) {
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getId() == mConstraintSetId && child instanceof Constraints) {
mConstraintSet = ((Constraints) child).getConstraintSet();
}
}
}
if (mConstraintSet != null) {
//这里主要是设置边界条件之类的
mConstraintSet.applyToInternal(this, true);
}
mLayoutWidget.removeAllChildren();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
ConstraintWidget widget = getViewWidget(child);
if (widget == null) {
continue;
}
final LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
mLayoutWidget.add(widget);
applyConstraintsFromLayoutParams(isInEditMode, child, widget, layoutParams, mTempMapIdToWidget);
}
}
最核心的方法为applyConstraintsFromLayoutParams(isInEditMode, child, widget, layoutParams, mTempMapIdToWidget);
protected void applyConstraintsFromLayoutParams(boolean isInEditMode,
View child,
ConstraintWidget widget, LayoutParams layoutParams,
SparseArray<ConstraintWidget> idToWidget) {
widget.setVisibility(child.getVisibility());
if (layoutParams.isInPlaceholder) {
widget.setInPlaceholder(true);
widget.setVisibility(View.GONE);
}
widget.setCompanionWidget(child);
if (child instanceof ConstraintHelper) {
ConstraintHelper helper = (ConstraintHelper) child;
helper.resolveRtl(widget, mLayoutWidget.isRtl());
}
if (layoutParams.isGuideline) {
//设置guideline的一些属性,这里先忽略
} else {
// Get the left/right constraints resolved for RTL
int resolvedLeftToLeft = layoutParams.resolvedLeftToLeft;
int resolvedLeftToRight = layoutParams.resolvedLeftToRight;
int resolvedRightToLeft = layoutParams.resolvedRightToLeft;
int resolvedRightToRight = layoutParams.resolvedRightToRight;
int resolveGoneLeftMargin = layoutParams.resolveGoneLeftMargin;
int resolveGoneRightMargin = layoutParams.resolveGoneRightMargin;
float resolvedHorizontalBias = layoutParams.resolvedHorizontalBias;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Pre JB MR1, left/right should take precedence, unless they are
// not defined and somehow a corresponding start/end constraint exists
resolvedLeftToLeft = layoutParams.leftToLeft;
resolvedLeftToRight = layoutParams.leftToRight;
resolvedRightToLeft = layoutParams.rightToLeft;
resolvedRightToRight = layoutParams.rightToRight;
resolveGoneLeftMargin = layoutParams.goneLeftMargin;
resolveGoneRightMargin = layoutParams.goneRightMargin;
resolvedHorizontalBias = layoutParams.horizontalBias;
if (resolvedLeftToLeft == UNSET && resolvedLeftToRight == UNSET) {
if (layoutParams.startToStart != UNSET) {
resolvedLeftToLeft = layoutParams.startToStart;
} else if (layoutParams.startToEnd != UNSET) {
resolvedLeftToRight = layoutParams.startToEnd;
}
}
if (resolvedRightToLeft == UNSET && resolvedRightToRight == UNSET) {
if (layoutParams.endToStart != UNSET) {
resolvedRightToLeft = layoutParams.endToStart;
} else if (layoutParams.endToEnd != UNSET) {
resolvedRightToRight = layoutParams.endToEnd;
}
}
}
// Circular constraint
if (layoutParams.circleConstraint != UNSET) {
ConstraintWidget target = idToWidget.get(layoutParams.circleConstraint);
if (target != null) {
widget.connectCircularConstraint(target, layoutParams.circleAngle, layoutParams.circleRadius);
}
} else {
// Left constraint
if (resolvedLeftToLeft != UNSET) {
ConstraintWidget target = idToWidget.get(resolvedLeftToLeft);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.LEFT, target,
ConstraintAnchor.Type.LEFT, layoutParams.leftMargin,
resolveGoneLeftMargin);
}
} else if (resolvedLeftToRight != UNSET) {
ConstraintWidget target = idToWidget.get(resolvedLeftToRight);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.LEFT, target,
ConstraintAnchor.Type.RIGHT, layoutParams.leftMargin,
resolveGoneLeftMargin);
}
}
// Right constraint
if (resolvedRightToLeft != UNSET) {
ConstraintWidget target = idToWidget.get(resolvedRightToLeft);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.RIGHT, target,
ConstraintAnchor.Type.LEFT, layoutParams.rightMargin,
resolveGoneRightMargin);
}
} else if (resolvedRightToRight != UNSET) {
ConstraintWidget target = idToWidget.get(resolvedRightToRight);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.RIGHT, target,
ConstraintAnchor.Type.RIGHT, layoutParams.rightMargin,
resolveGoneRightMargin);
}
}
// Top constraint
if (layoutParams.topToTop != UNSET) {
ConstraintWidget target = idToWidget.get(layoutParams.topToTop);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.TOP, target,
ConstraintAnchor.Type.TOP, layoutParams.topMargin,
layoutParams.goneTopMargin);
}
} else if (layoutParams.topToBottom != UNSET) {
ConstraintWidget target = idToWidget.get(layoutParams.topToBottom);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.TOP, target,
ConstraintAnchor.Type.BOTTOM, layoutParams.topMargin,
layoutParams.goneTopMargin);
}
}
// Bottom constraint
if (layoutParams.bottomToTop != UNSET) {
ConstraintWidget target = idToWidget.get(layoutParams.bottomToTop);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.BOTTOM, target,
ConstraintAnchor.Type.TOP, layoutParams.bottomMargin,
layoutParams.goneBottomMargin);
}
} else if (layoutParams.bottomToBottom != UNSET) {
ConstraintWidget target = idToWidget.get(layoutParams.bottomToBottom);
if (target != null) {
widget.immediateConnect(ConstraintAnchor.Type.BOTTOM, target,
ConstraintAnchor.Type.BOTTOM, layoutParams.bottomMargin,
layoutParams.goneBottomMargin);
}
}
// Baseline constraint
if (layoutParams.baselineToBaseline != UNSET) {
setWidgetBaseline(widget, layoutParams, idToWidget,
layoutParams.baselineToBaseline, ConstraintAnchor.Type.BASELINE);
} else if (layoutParams.baselineToTop != UNSET) {
setWidgetBaseline(widget, layoutParams, idToWidget,
layoutParams.baselineToTop, ConstraintAnchor.Type.TOP);
} else if (layoutParams.baselineToBottom != UNSET) {
setWidgetBaseline(widget, layoutParams, idToWidget,
layoutParams.baselineToBottom, ConstraintAnchor.Type.BOTTOM);
}
if (resolvedHorizontalBias >= 0) {
widget.setHorizontalBiasPercent(resolvedHorizontalBias);
}
if (layoutParams.verticalBias >= 0) {
widget.setVerticalBiasPercent(layoutParams.verticalBias);
}
}
if (isInEditMode && ((layoutParams.editorAbsoluteX != UNSET)
|| (layoutParams.editorAbsoluteY != UNSET))) {
widget.setOrigin(layoutParams.editorAbsoluteX, layoutParams.editorAbsoluteY);
}
// FIXME: need to agree on the correct magic value for this rather than simply using zero.
if (!layoutParams.horizontalDimensionFixed) {
if (layoutParams.width == MATCH_PARENT) {
if (layoutParams.constrainedWidth) {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
} else {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_PARENT);
}
widget.getAnchor(ConstraintAnchor.Type.LEFT).mMargin = layoutParams.leftMargin;
widget.getAnchor(ConstraintAnchor.Type.RIGHT).mMargin = layoutParams.rightMargin;
} else {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
widget.setWidth(0);
}
} else {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
widget.setWidth(layoutParams.width);
if (layoutParams.width == WRAP_CONTENT) {
widget.setHorizontalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
}
}
if (!layoutParams.verticalDimensionFixed) {
if (layoutParams.height == MATCH_PARENT) {
if (layoutParams.constrainedHeight) {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
} else {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_PARENT);
}
widget.getAnchor(ConstraintAnchor.Type.TOP).mMargin = layoutParams.topMargin;
widget.getAnchor(ConstraintAnchor.Type.BOTTOM).mMargin = layoutParams.bottomMargin;
} else {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.MATCH_CONSTRAINT);
widget.setHeight(0);
}
} else {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.FIXED);
widget.setHeight(layoutParams.height);
if (layoutParams.height == WRAP_CONTENT) {
widget.setVerticalDimensionBehaviour(ConstraintWidget.DimensionBehaviour.WRAP_CONTENT);
}
}
widget.setDimensionRatio(layoutParams.dimensionRatio);
widget.setHorizontalWeight(layoutParams.horizontalWeight);
widget.setVerticalWeight(layoutParams.verticalWeight);
widget.setHorizontalChainStyle(layoutParams.horizontalChainStyle);
widget.setVerticalChainStyle(layoutParams.verticalChainStyle);
widget.setWrapBehaviorInParent(layoutParams.wrapBehaviorInParent);
widget.setHorizontalMatchStyle(layoutParams.matchConstraintDefaultWidth,
layoutParams.matchConstraintMinWidth, layoutParams.matchConstraintMaxWidth,
layoutParams.matchConstraintPercentWidth);
widget.setVerticalMatchStyle(layoutParams.matchConstraintDefaultHeight,
layoutParams.matchConstraintMinHeight, layoutParams.matchConstraintMaxHeight,
layoutParams.matchConstraintPercentHeight);
}
}
这里的核心方法为 ConstraintWidget 中的immediateConnect 方法,最终调用了其connect方法
public void connect(ConstraintAnchor.Type constraintFrom,
ConstraintWidget target,
ConstraintAnchor.Type constraintTo, int margin) {
if (constraintFrom == ConstraintAnchor.Type.CENTER) {
// If we have center, we connect instead to the corresponding
// left/right or top/bottom pairs
if (constraintTo == ConstraintAnchor.Type.CENTER) {
ConstraintAnchor left = getAnchor(ConstraintAnchor.Type.LEFT);
ConstraintAnchor right = getAnchor(ConstraintAnchor.Type.RIGHT);
ConstraintAnchor top = getAnchor(ConstraintAnchor.Type.TOP);
ConstraintAnchor bottom = getAnchor(ConstraintAnchor.Type.BOTTOM);
boolean centerX = false;
boolean centerY = false;
if ((left != null && left.isConnected())
|| (right != null && right.isConnected())) {
// don't apply center here
} else {
connect(ConstraintAnchor.Type.LEFT, target,
ConstraintAnchor.Type.LEFT, 0);
connect(ConstraintAnchor.Type.RIGHT, target,
ConstraintAnchor.Type.RIGHT, 0);
centerX = true;
}
if ((top != null && top.isConnected())
|| (bottom != null && bottom.isConnected())) {
// don't apply center here
} else {
connect(ConstraintAnchor.Type.TOP, target,
ConstraintAnchor.Type.TOP, 0);
connect(ConstraintAnchor.Type.BOTTOM, target,
ConstraintAnchor.Type.BOTTOM, 0);
centerY = true;
}
if (centerX && centerY) {
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER);
center.connect(target.getAnchor(ConstraintAnchor.Type.CENTER), 0);
} else if (centerX) {
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER_X);
center.connect(target.getAnchor(ConstraintAnchor.Type.CENTER_X), 0);
} else if (centerY) {
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER_Y);
center.connect(target.getAnchor(ConstraintAnchor.Type.CENTER_Y), 0);
}
} else if ((constraintTo == ConstraintAnchor.Type.LEFT)
|| (constraintTo == ConstraintAnchor.Type.RIGHT)) {
connect(ConstraintAnchor.Type.LEFT, target,
constraintTo, 0);
connect(ConstraintAnchor.Type.RIGHT, target,
constraintTo, 0);
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER);
center.connect(target.getAnchor(constraintTo), 0);
} else if ((constraintTo == ConstraintAnchor.Type.TOP)
|| (constraintTo == ConstraintAnchor.Type.BOTTOM)) {
connect(ConstraintAnchor.Type.TOP, target,
constraintTo, 0);
connect(ConstraintAnchor.Type.BOTTOM, target,
constraintTo, 0);
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER);
center.connect(target.getAnchor(constraintTo), 0);
}
} else if (constraintFrom == ConstraintAnchor.Type.CENTER_X
&& (constraintTo == ConstraintAnchor.Type.LEFT
|| constraintTo == ConstraintAnchor.Type.RIGHT)) {
ConstraintAnchor left = getAnchor(ConstraintAnchor.Type.LEFT);
ConstraintAnchor targetAnchor = target.getAnchor(constraintTo);
ConstraintAnchor right = getAnchor(ConstraintAnchor.Type.RIGHT);
left.connect(targetAnchor, 0);
right.connect(targetAnchor, 0);
ConstraintAnchor centerX = getAnchor(ConstraintAnchor.Type.CENTER_X);
centerX.connect(targetAnchor, 0);
} else if (constraintFrom == ConstraintAnchor.Type.CENTER_Y
&& (constraintTo == ConstraintAnchor.Type.TOP
|| constraintTo == ConstraintAnchor.Type.BOTTOM)) {
ConstraintAnchor targetAnchor = target.getAnchor(constraintTo);
ConstraintAnchor top = getAnchor(ConstraintAnchor.Type.TOP);
top.connect(targetAnchor, 0);
ConstraintAnchor bottom = getAnchor(ConstraintAnchor.Type.BOTTOM);
bottom.connect(targetAnchor, 0);
ConstraintAnchor centerY = getAnchor(ConstraintAnchor.Type.CENTER_Y);
centerY.connect(targetAnchor, 0);
} else if (constraintFrom == ConstraintAnchor.Type.CENTER_X
&& constraintTo == ConstraintAnchor.Type.CENTER_X) {
// Center X connection will connect left & right
ConstraintAnchor left = getAnchor(ConstraintAnchor.Type.LEFT);
ConstraintAnchor leftTarget = target.getAnchor(ConstraintAnchor.Type.LEFT);
left.connect(leftTarget, 0);
ConstraintAnchor right = getAnchor(ConstraintAnchor.Type.RIGHT);
ConstraintAnchor rightTarget = target.getAnchor(ConstraintAnchor.Type.RIGHT);
right.connect(rightTarget, 0);
ConstraintAnchor centerX = getAnchor(ConstraintAnchor.Type.CENTER_X);
centerX.connect(target.getAnchor(constraintTo), 0);
} else if (constraintFrom == ConstraintAnchor.Type.CENTER_Y
&& constraintTo == ConstraintAnchor.Type.CENTER_Y) {
// Center Y connection will connect top & bottom.
ConstraintAnchor top = getAnchor(ConstraintAnchor.Type.TOP);
ConstraintAnchor topTarget = target.getAnchor(ConstraintAnchor.Type.TOP);
top.connect(topTarget, 0);
ConstraintAnchor bottom = getAnchor(ConstraintAnchor.Type.BOTTOM);
ConstraintAnchor bottomTarget = target.getAnchor(ConstraintAnchor.Type.BOTTOM);
bottom.connect(bottomTarget, 0);
ConstraintAnchor centerY = getAnchor(ConstraintAnchor.Type.CENTER_Y);
centerY.connect(target.getAnchor(constraintTo), 0);
} else {
ConstraintAnchor fromAnchor = getAnchor(constraintFrom);
ConstraintAnchor toAnchor = target.getAnchor(constraintTo);
if (fromAnchor.isValidConnection(toAnchor)) {
// make sure that the baseline takes precedence over top/bottom
// and reversely, reset the baseline if we are connecting top/bottom
if (constraintFrom == ConstraintAnchor.Type.BASELINE) {
ConstraintAnchor top = getAnchor(ConstraintAnchor.Type.TOP);
ConstraintAnchor bottom = getAnchor(ConstraintAnchor.Type.BOTTOM);
if (top != null) {
top.reset();
}
if (bottom != null) {
bottom.reset();
}
} else if ((constraintFrom == ConstraintAnchor.Type.TOP)
|| (constraintFrom == ConstraintAnchor.Type.BOTTOM)) {
ConstraintAnchor baseline = getAnchor(ConstraintAnchor.Type.BASELINE);
if (baseline != null) {
baseline.reset();
}
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER);
if (center.getTarget() != toAnchor) {
center.reset();
}
ConstraintAnchor opposite = getAnchor(constraintFrom).getOpposite();
ConstraintAnchor centerY = getAnchor(ConstraintAnchor.Type.CENTER_Y);
if (centerY.isConnected()) {
opposite.reset();
centerY.reset();
} else {
if (AUTOTAG_CENTER) {
// let's see if we need to mark center_y as connected
if (opposite.isConnected() && opposite.getTarget().getOwner()
== toAnchor.getOwner()) {
ConstraintAnchor targetCenterY = toAnchor.getOwner().getAnchor(
ConstraintAnchor.Type.CENTER_Y);
centerY.connect(targetCenterY, 0);
}
}
}
} else if ((constraintFrom == ConstraintAnchor.Type.LEFT)
|| (constraintFrom == ConstraintAnchor.Type.RIGHT)) {
ConstraintAnchor center = getAnchor(ConstraintAnchor.Type.CENTER);
if (center.getTarget() != toAnchor) {
center.reset();
}
ConstraintAnchor opposite = getAnchor(constraintFrom).getOpposite();
ConstraintAnchor centerX = getAnchor(ConstraintAnchor.Type.CENTER_X);
if (centerX.isConnected()) {
opposite.reset();
centerX.reset();
} else {
if (AUTOTAG_CENTER) {
// let's see if we need to mark center_x as connected
if (opposite.isConnected() && opposite.getTarget().getOwner()
== toAnchor.getOwner()) {
ConstraintAnchor targetCenterX = toAnchor.getOwner().getAnchor(
ConstraintAnchor.Type.CENTER_X);
centerX.connect(targetCenterX, 0);
}
}
}
}
fromAnchor.connect(toAnchor, margin);
}
}
}
在这个connect方法里面,对控件进行上下左右的约束连接。这个方法真是经典,一行代码都不忍心删了。