private void createViewMetrics(Stack<ViewConstraints> springMetrics) {
    springMetrics.clear();
    mIdToViewConstraints.clear();

    if (mRootConstraints != null) {
      mRootConstraints.release();
      for (ViewConstraints mViewConstraint : mViewConstraints) {
        mViewConstraint.release();
      }

      mRootConstraints.reset(this);
      resizeViewConstraintsArray(getChildCount());
    } else {
      mRootConstraints = new ViewConstraints(this, mLayoutMath);
      mViewConstraints = new ViewConstraints[getChildCount()];
    }

    mRootConstraints.left.setValueObject(mLayoutMath.variable(0));
    mRootConstraints.top.setValueObject(mLayoutMath.variable(0));

    final int count = getChildCount();

    for (int i = 0; i < count; i++) {
      final View v = getChildAt(i);
      mIdToViewConstraints.append(v.getId(), i);
      if (mViewConstraints[i] == null) {
        mViewConstraints[i] = new ViewConstraints(v, mLayoutMath);
      } else {
        mViewConstraints[i].reset(v);
      }
    }

    for (int i = 0; i < count; i++) {
      final ViewConstraints viewConstraints = mViewConstraints[i];
      final LayoutParams layoutParams = (LayoutParams) viewConstraints.getView().getLayoutParams();

      if (layoutParams.getWidthWeight() > 0) {
        viewConstraints.markAsHorizontalSpring();
      }

      if (layoutParams.getHeightWeight() > 0) {
        viewConstraints.markAsVerticalSpring();
      }

      int[] childRules = layoutParams.getRelations();
      for (int relation : VALID_RELATIONS) {
        final ViewConstraints metrics = getViewMetrics(childRules[relation]);
        if (metrics != null) {
          metrics.updateRelation(viewConstraints, relation);
        }
      }
      if (viewConstraints.isHorizontalSpring() || viewConstraints.isVerticalSpring()) {
        springMetrics.add(viewConstraints);
      }
    }
  }
  private void adaptLayoutParameters() {
    int count = getChildCount();
    for (int i = 0; i < count; i++) {
      final View child = getChildAt(i);
      final LayoutParams childParams = (LayoutParams) child.getLayoutParams();
      int[] relations = childParams.getRelations();

      if (childParams.getWidthWeight() > 0 && childParams.width != LayoutParams.WRAP_CONTENT) {
        throw new IllegalArgumentException(
            "widthWeight > 0 not supported for layout_width != WRAP_CONTENT in View: " + child);
      }

      if (childParams.getHeightWeight() > 0 && childParams.height != LayoutParams.WRAP_CONTENT) {
        throw new IllegalArgumentException(
            "heightWeight > 0 not supported for layout_height != WRAP_CONTENT in View: " + child);
      }

      // If view is aligned both to parent's top and bottom (left and
      // right) then its height (width) is MATCH_PARENT and the other way
      // around
      if (relations[ALIGN_PARENT_TOP] != 0 && relations[ALIGN_PARENT_BOTTOM] != 0) {
        childParams.height = LayoutParams.MATCH_PARENT;
      } else if (childParams.height == LayoutParams.MATCH_PARENT) {
        relations[ALIGN_PARENT_TOP] = relations[ALIGN_PARENT_BOTTOM] = TRUE;
      }

      if (relations[ALIGN_PARENT_LEFT] != 0 && relations[ALIGN_PARENT_RIGHT] != 0) {
        childParams.width = LayoutParams.MATCH_PARENT;
      } else if (childParams.width == LayoutParams.MATCH_PARENT) {
        relations[ALIGN_PARENT_LEFT] = relations[ALIGN_PARENT_RIGHT] = TRUE;
      }

      if (relations[ALIGN_PARENT_TOP] == TRUE) {
        relations[ALIGN_TOP] = PARENT;
      }

      if (relations[ALIGN_PARENT_BOTTOM] == TRUE) {
        relations[ALIGN_BOTTOM] = PARENT;
      }

      if (relations[ALIGN_PARENT_LEFT] == TRUE) {
        relations[ALIGN_LEFT] = PARENT;
      }

      if (relations[ALIGN_PARENT_RIGHT] == TRUE) {
        relations[ALIGN_RIGHT] = PARENT;
      }

      if (relations[ALIGN_CENTER] != 0) {
        relations[ALIGN_CENTER_HORIZONTALLY] = relations[ALIGN_CENTER];
        relations[ALIGN_CENTER_VERTICALLY] = relations[ALIGN_CENTER];
      }

      if (relations[CENTER_IN_PARENT] == TRUE) {
        relations[CENTER_HORIZONTAL] = relations[CENTER_VERTICAL] = TRUE;
      }

      if (relations[CENTER_HORIZONTAL] == TRUE) {
        relations[ALIGN_CENTER_HORIZONTALLY] = PARENT;
      }

      if (relations[CENTER_VERTICAL] == TRUE) {
        relations[ALIGN_CENTER_VERTICALLY] = PARENT;
      }

      if (!hasHorizontalRelations(relations)) {
        relations[ALIGN_LEFT] = PARENT;
      }

      if (!hasVerticalRelations(relations)) {
        relations[ALIGN_TOP] = PARENT;
      }
    }
  }