private int shrinkFlexItems(FlexLine flexLine,@FlexDirection int flexDirection,int maxMainSize,int paddingAlongMainAxis,int startIndex){
  int childIndex=startIndex;
  int sizeBeforeShrink=flexLine.mMainSize;
  if (flexLine.mTotalFlexShrink <= 0 || maxMainSize > flexLine.mMainSize) {
    childIndex+=flexLine.mItemCount;
    return childIndex;
  }
  boolean needsReshrink=false;
  float unitShrink=(flexLine.mMainSize - maxMainSize) / flexLine.mTotalFlexShrink;
  float accumulatedRoundError=0;
  flexLine.mMainSize=paddingAlongMainAxis + flexLine.mDividerLengthInMainSize;
  for (int i=0; i < flexLine.mItemCount; i++) {
    View child=getReorderedChildAt(childIndex);
    if (child == null) {
      continue;
    }
 else     if (child.getVisibility() == View.GONE) {
      childIndex++;
      continue;
    }
    LayoutParams lp=(LayoutParams)child.getLayoutParams();
    if (isMainAxisDirectionHorizontal(flexDirection)) {
      if (!mChildrenFrozen[childIndex]) {
        float rawCalculatedWidth=child.getMeasuredWidth() - unitShrink * lp.flexShrink;
        if (i == flexLine.mItemCount - 1) {
          rawCalculatedWidth+=accumulatedRoundError;
          accumulatedRoundError=0;
        }
        int newWidth=Math.round(rawCalculatedWidth);
        if (newWidth < lp.minWidth) {
          needsReshrink=true;
          newWidth=lp.minWidth;
          mChildrenFrozen[childIndex]=true;
          flexLine.mTotalFlexShrink-=lp.flexShrink;
        }
 else {
          accumulatedRoundError+=(rawCalculatedWidth - newWidth);
          if (accumulatedRoundError > 1.0) {
            newWidth+=1;
            accumulatedRoundError-=1;
          }
 else           if (accumulatedRoundError < -1.0) {
            newWidth-=1;
            accumulatedRoundError+=1;
          }
        }
        child.measure(MeasureSpec.makeMeasureSpec(newWidth,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(),MeasureSpec.EXACTLY));
      }
      flexLine.mMainSize+=child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
    }
 else {
      if (!mChildrenFrozen[childIndex]) {
        float rawCalculatedHeight=child.getMeasuredHeight() - unitShrink * lp.flexShrink;
        if (i == flexLine.mItemCount - 1) {
          rawCalculatedHeight+=accumulatedRoundError;
          accumulatedRoundError=0;
        }
        int newHeight=Math.round(rawCalculatedHeight);
        if (newHeight < lp.minHeight) {
          needsReshrink=true;
          newHeight=lp.minHeight;
          mChildrenFrozen[childIndex]=true;
          flexLine.mTotalFlexShrink-=lp.flexShrink;
        }
 else {
          accumulatedRoundError+=(rawCalculatedHeight - newHeight);
          if (accumulatedRoundError > 1.0) {
            newHeight+=1;
            accumulatedRoundError-=1;
          }
 else           if (accumulatedRoundError < -1.0) {
            newHeight-=1;
            accumulatedRoundError+=1;
          }
        }
        child.measure(MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(),MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(newHeight,MeasureSpec.EXACTLY));
      }
      flexLine.mMainSize+=child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
    }
    childIndex++;
  }
  if (needsReshrink && sizeBeforeShrink != flexLine.mMainSize) {
    shrinkFlexItems(flexLine,flexDirection,maxMainSize,paddingAlongMainAxis,startIndex);
  }
  return childIndex;
}

private int shrinkFlexItems ( FlexLine flexLine , @ FlexDirection int flexDirection , int maxMainSize , int paddingAlongMainAxis , int startIndex ) { int childIndex = startIndex ; int sizeBeforeShrink = flexLine . mMainSize ; if ( flexLine . mTotalFlexShrink <= 0 || maxMainSize > flexLine . mMainSize ) { childIndex += flexLine . mItemCount ; return childIndex ; } boolean needsReshrink = false ; float unitShrink = ( flexLine . mMainSize - maxMainSize ) / flexLine . mTotalFlexShrink ; float accumulatedRoundError = 0 ; flexLine . mMainSize = paddingAlongMainAxis + flexLine . mDividerLengthInMainSize ; for ( int i = 0 ; i < flexLine . mItemCount ; i ++ ) { View child = getReorderedChildAt ( childIndex ) ; if ( child == null ) { continue ; } else if ( child . getVisibility ( ) == View . GONE ) { childIndex ++ ; continue ; } LayoutParams lp = ( LayoutParams ) child . getLayoutParams ( ) ; if ( isMainAxisDirectionHorizontal ( flexDirection ) ) { if ( ! mChildrenFrozen [ childIndex ] ) { float rawCalculatedWidth = child . getMeasuredWidth ( ) - unitShrink * lp . flexShrink ; if ( i == flexLine . mItemCount - 1 ) { rawCalculatedWidth += accumulatedRoundError ; accumulatedRoundError = 0 ; } int newWidth = Math . round ( rawCalculatedWidth ) ; if ( newWidth < lp . minWidth ) { needsReshrink = true ; newWidth = lp . minWidth ; mChildrenFrozen [ childIndex ] = true ; flexLine . mTotalFlexShrink -= lp . flexShrink ; } else { accumulatedRoundError += ( rawCalculatedWidth - newWidth ) ; if ( accumulatedRoundError > 1.0 ) { newWidth += 1 ; accumulatedRoundError -= 1 ; } else if ( accumulatedRoundError < - 1.0 ) { newWidth -= 1 ; accumulatedRoundError += 1 ; } } child . measure ( MeasureSpec . makeMeasureSpec ( newWidth , MeasureSpec . EXACTLY ) , MeasureSpec . makeMeasureSpec ( child . getMeasuredHeight ( ) , MeasureSpec . EXACTLY ) ) ; } flexLine . mMainSize += child . getMeasuredWidth ( ) + lp . leftMargin + lp . rightMargin ; } else { if ( ! mChildrenFrozen [ childIndex ] ) { float rawCalculatedHeight = child . getMeasuredHeight ( ) - unitShrink * lp . flexShrink ; if ( i == flexLine . mItemCount - 1 ) { rawCalculatedHeight += accumulatedRoundError ; accumulatedRoundError = 0 ; } int newHeight = Math . round ( rawCalculatedHeight ) ; if ( newHeight < lp . minHeight ) { needsReshrink = true ; newHeight = lp . minHeight ; mChildrenFrozen [ childIndex ] = true ; flexLine . mTotalFlexShrink -= lp . flexShrink ; } else { accumulatedRoundError += ( rawCalculatedHeight - newHeight ) ; if ( accumulatedRoundError > 1.0 ) { newHeight += 1 ; accumulatedRoundError -= 1 ; } else if ( accumulatedRoundError < - 1.0 ) { newHeight -= 1 ; accumulatedRoundError += 1 ; } } child . measure ( MeasureSpec . makeMeasureSpec ( child . getMeasuredWidth ( ) , MeasureSpec . EXACTLY ) , MeasureSpec . makeMeasureSpec ( newHeight , MeasureSpec . EXACTLY ) ) ; } flexLine . mMainSize += child . getMeasuredHeight ( ) + lp . topMargin + lp . bottomMargin ; } childIndex ++ ; } if ( needsReshrink && sizeBeforeShrink != flexLine . mMainSize ) { shrinkFlexItems ( flexLine , flexDirection , maxMainSize , paddingAlongMainAxis , startIndex ) ; } return childIndex ; }
shrink the flex items along the main axis based on the individual flexshrink attribute .
