public static void applyGravityToLine(LineDefinition line, ConfigDefinition config) { final List<ViewDefinition> views = line.getViews(); final int viewCount = views.size(); if (viewCount <= 0) { return; } float totalWeight = 0; for (int i = 0; i < viewCount; i++) { final ViewDefinition child = views.get(i); totalWeight += getWeight(child, config); } ViewDefinition lastChild = views.get(viewCount - 1); int excessLength = line.getLineLength() - (lastChild.getLength() + lastChild.getSpacingLength() + lastChild.getInlineStartLength()); int excessOffset = 0; for (int i = 0; i < viewCount; i++) { final ViewDefinition child = views.get(i); float weight = getWeight(child, config); int gravity = getGravity(child, config); int extraLength; if (totalWeight == 0) { extraLength = excessLength / viewCount; } else { extraLength = Math.round(excessLength * weight / totalWeight); } final int childLength = child.getLength() + child.getSpacingLength(); final int childThickness = child.getThickness() + child.getSpacingThickness(); Rect container = new Rect(); container.top = 0; container.left = excessOffset; container.right = childLength + extraLength + excessOffset; container.bottom = line.getLineThickness(); Rect result = new Rect(); Gravity.apply(gravity, childLength, childThickness, container, result); excessOffset += extraLength; child.setInlineStartLength(result.left + child.getInlineStartLength()); child.setInlineStartThickness(result.top); child.setLength(result.width() - child.getSpacingLength()); child.setThickness(result.height() - child.getSpacingThickness()); } }
public static void calculateLinesAndChildPosition(List<LineDefinition> lines) { int prevLinesThickness = 0; final int linesCount = lines.size(); for (int i = 0; i < linesCount; i++) { final LineDefinition line = lines.get(i); line.setLineStartThickness(prevLinesThickness); prevLinesThickness += line.getLineThickness(); int prevChildThickness = 0; final List<ViewDefinition> childViews = line.getViews(); final int childCount = childViews.size(); for (int j = 0; j < childCount; j++) { ViewDefinition child = childViews.get(j); child.setInlineStartLength(prevChildThickness); prevChildThickness += child.getLength() + child.getSpacingLength(); } } }