Fix GridLayout rounding issue

Fix TextView text centering
This commit is contained in:
hshristov
2015-08-10 14:32:24 +03:00
parent c0d47c8c3f
commit 467353d3d5
2 changed files with 28 additions and 22 deletions

View File

@@ -126,14 +126,18 @@ public class CommonLayoutParams extends FrameLayout.LayoutParams {
// Re-measure TextView because it is not centered if layout width is larger than measure width. // Re-measure TextView because it is not centered if layout width is larger than measure width.
if (child instanceof android.widget.TextView) { if (child instanceof android.widget.TextView) {
boolean canChangeWidth = lp.width < 0;
boolean canChangeHeight = lp.height < 0;
int measuredWidth = child.getMeasuredWidth(); int measuredWidth = child.getMeasuredWidth();
int measuredHeight = child.getMeasuredHeight(); int measuredHeight = child.getMeasuredHeight();
int width = right - left; int width = childRight - childLeft;
int height = bottom - top; int height = childBottom - childTop;
if (Math.abs(measuredWidth - width) > 1 || Math.abs(measuredHeight - height) > 1) { if ((Math.abs(measuredWidth - width) > 1 && canChangeWidth) || (Math.abs(measuredHeight - height) > 1 && canChangeHeight)) {
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(canChangeWidth ? width : lp.width, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(canChangeHeight ? height : lp.height, MeasureSpec.EXACTLY);
if (debuggable > 0) { if (debuggable > 0) {
sb.setLength(0); sb.setLength(0);
sb.append("remeasure "); sb.append("remeasure ");

View File

@@ -302,23 +302,24 @@ public class GridLayout extends LayoutBase {
this.columnOffsets.clear(); this.columnOffsets.clear();
this.rowOffsets.clear(); this.rowOffsets.clear();
this.columnOffsets.add(paddingLeft); this.columnOffsets.add(paddingLeft);
this.rowOffsets.add(paddingTop); this.rowOffsets.add(paddingTop);
int offset = this.columnOffsets.get(0); float offset = this.columnOffsets.get(0);
int roundedOffset = this.getPaddingLeft(); int roundedOffset = paddingLeft;
int roundedLength = 0; int roundedLength = 0;
int actualLength = 0; float actualLength = 0;
int size = this.helper.columns.size(); int size = this.helper.columns.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ItemGroup columnGroup = this.helper.columns.get(i); ItemGroup columnGroup = this.helper.columns.get(i);
offset += columnGroup.length; offset += columnGroup.length;
this.columnOffsets.add(offset);
actualLength = offset - roundedOffset; actualLength = offset - roundedOffset;
roundedLength = Math.round(actualLength); roundedLength = Math.round(actualLength);
columnGroup.rowOrColumn._actualLength = roundedLength; columnGroup.rowOrColumn._actualLength = roundedLength;
roundedOffset += roundedLength; roundedOffset += roundedLength;
this.columnOffsets.add(roundedOffset);
} }
offset = this.rowOffsets.get(0); offset = this.rowOffsets.get(0);
@@ -329,12 +330,13 @@ public class GridLayout extends LayoutBase {
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
ItemGroup rowGroup = this.helper.rows.get(i); ItemGroup rowGroup = this.helper.rows.get(i);
offset += rowGroup.length; offset += rowGroup.length;
this.rowOffsets.add(offset);
actualLength = offset - roundedOffset; actualLength = offset - roundedOffset;
roundedLength = Math.round(actualLength); roundedLength = Math.round(actualLength);
rowGroup.rowOrColumn._actualLength = roundedLength; rowGroup.rowOrColumn._actualLength = roundedLength;
roundedOffset += roundedLength; roundedOffset += roundedLength;
this.rowOffsets.add(roundedOffset);
} }
int columns = this.helper.columns.size(); int columns = this.helper.columns.size();
@@ -442,7 +444,7 @@ class MeasureSpecs {
} }
class ItemGroup { class ItemGroup {
int length = 0; float length = 0;
int measuredCount = 0; int measuredCount = 0;
ItemSpec rowOrColumn; ItemSpec rowOrColumn;
ArrayList<MeasureSpecs> children = new ArrayList<MeasureSpecs>(); ArrayList<MeasureSpecs> children = new ArrayList<MeasureSpecs>();
@@ -506,8 +508,8 @@ class MeasureHelper {
int measuredWidth; int measuredWidth;
int measuredHeight; int measuredHeight;
private int columnStarValue; private float columnStarValue;
private int rowStarValue; private float rowStarValue;
private boolean fakeRowAdded = false; private boolean fakeRowAdded = false;
private boolean fakeColumnAdded = false; private boolean fakeColumnAdded = false;
@@ -681,7 +683,7 @@ class MeasureHelper {
} }
private void fixColumns() { private void fixColumns() {
int currentColumnWidth = 0; float currentColumnWidth = 0;
int columnStarCount = 0; int columnStarCount = 0;
int columnCount = this.columns.size(); int columnCount = this.columns.size();
@@ -706,7 +708,7 @@ class MeasureHelper {
} }
private void fixRows() { private void fixRows() {
int currentRowHeight = 0; float currentRowHeight = 0;
int rowStarCount = 0; int rowStarCount = 0;
int rowCount = this.rows.size(); int rowCount = this.rows.size();
@@ -955,7 +957,7 @@ class MeasureHelper {
} }
} }
int measureWidth = columnsWidth + measureSpec.starColumnsCount * this.columnStarValue; int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, this.stretchedHorizontally ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, this.stretchedHorizontally ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
int heightMeasureSpec = (measureSpec.autoRowsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelHeight, MeasureSpec.EXACTLY); int heightMeasureSpec = (measureSpec.autoRowsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelHeight, MeasureSpec.EXACTLY);
@@ -1020,7 +1022,7 @@ class MeasureHelper {
} }
} }
int measureHeight = rowsHeight + measureSpec.starRowsCount * this.rowStarValue; int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
int widthMeasureSpec = (measureSpec.autoColumnsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelWidth, MeasureSpec.EXACTLY); int widthMeasureSpec = (measureSpec.autoColumnsCount > 0) ? infinity : MeasureSpec.makeMeasureSpec(measureSpec.pixelWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, this.stretchedVertically ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, this.stretchedVertically ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST);
@@ -1084,7 +1086,7 @@ class MeasureHelper {
ItemGroup columnGroup; ItemGroup columnGroup;
ItemGroup rowGroup; ItemGroup rowGroup;
int columnsWidth = 0; float columnsWidth = 0;
for (int i = columnIndex; i < columnSpanEnd; i++) { for (int i = columnIndex; i < columnSpanEnd; i++) {
columnGroup = this.columns.get(i); columnGroup = this.columns.get(i);
if (!columnGroup.getIsStar()) { if (!columnGroup.getIsStar()) {
@@ -1092,7 +1094,7 @@ class MeasureHelper {
} }
} }
int rowsHeight = 0; float rowsHeight = 0;
for (int i = rowIndex; i < rowSpanEnd; i++) { for (int i = rowIndex; i < rowSpanEnd; i++) {
rowGroup = this.rows.get(i); rowGroup = this.rows.get(i);
if (!rowGroup.getIsStar()) { if (!rowGroup.getIsStar()) {
@@ -1100,8 +1102,8 @@ class MeasureHelper {
} }
} }
int measureWidth = columnsWidth + measureSpec.starColumnsCount * this.columnStarValue; int measureWidth = (int)(columnsWidth + measureSpec.starColumnsCount * this.columnStarValue);
int measureHeight = rowsHeight + measureSpec.starRowsCount * this.rowStarValue; int measureHeight = (int)(rowsHeight + measureSpec.starRowsCount * this.rowStarValue);
// if (have stars) & (not stretch) - at most // if (have stars) & (not stretch) - at most
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth,