/** * Computes a horizontal "gap" match - a preferred distance from the nearest edge, including * margin edges */ private void addColumnGapMatch( Rect bounds, int x1, int x2, List<GridMatch> columnMatches, int max) { if (x1 < bounds.x + MARGIN_SIZE + max) { int matchedLine = bounds.x + MARGIN_SIZE; int distance = abs(matchedLine - x1); if (distance <= max) { boolean createCell = mGrid.getColumnX(mGrid.getColumn(matchedLine)) != matchedLine; columnMatches.add( new GridMatch(SegmentType.LEFT, distance, matchedLine, 0, createCell, MARGIN_SIZE)); } } else if (x2 > bounds.x2() - MARGIN_SIZE - max) { int matchedLine = bounds.x2() - MARGIN_SIZE; int distance = abs(matchedLine - x2); if (distance <= max) { // This does not yet work properly; we need to use columnWeights to achieve this // boolean createCell = mGrid.getColumnX(mGrid.getColumn(matchedLine)) != matchedLine; // columnMatches.add(new GridMatch(SegmentType.RIGHT, distance, matchedLine, // mGrid.actualColumnCount - 1, createCell, MARGIN_SIZE)); } } else { int columnRight = mGrid.getColumn(x1 - SHORT_GAP_DP); int columnX = mGrid.getColumnMaxX(columnRight); int matchedLine = columnX + SHORT_GAP_DP; int distance = abs(matchedLine - x1); if (distance <= max) { boolean createCell = mGrid.getColumnX(mGrid.getColumn(matchedLine)) != matchedLine; columnMatches.add( new GridMatch( SegmentType.LEFT, distance, matchedLine, columnRight, createCell, SHORT_GAP_DP)); } // Add a column directly adjacent (no gap) columnRight = mGrid.getColumn(x1); columnX = mGrid.getColumnMaxX(columnRight); matchedLine = columnX; distance = abs(matchedLine - x1); // Let's say you have this arrangement: // [button1][button2] // This is two columns, where the right hand side edge of column 1 is // flush with the left side edge of column 2, because in fact the width of // button1 is what defines the width of column 1, and that in turn is what // defines the left side position of column 2. // // In this case we don't want to consider inserting a new column at the // right hand side of button1 a better match than matching left on column 2. // Therefore, to ensure that this doesn't happen, we "penalize" right column // matches such that they don't get preferential treatment when the matching // line is on the left side of the column. distance += 2; if (distance <= max) { boolean createCell = mGrid.getColumnX(mGrid.getColumn(matchedLine)) != matchedLine; columnMatches.add( new GridMatch(SegmentType.LEFT, distance, matchedLine, columnRight, createCell, 0)); } } }
public final void testX2Y2() { Rect r = new Rect(1, 2, 3, 4); assertEquals(4, r.x2()); assertEquals(6, r.y2()); }
/** * Computes the best horizontal and vertical matches for a drag to the given position. * * @param feedback a {@link DropFeedback} object containing drag state like the drag bounds and * the drag baseline * @param p the mouse position */ public void computeMatches(DropFeedback feedback, Point p) { mRowMatch = mColumnMatch = null; feedback.tooltip = null; Rect bounds = mGrid.layout.getBounds(); int x1 = p.x; int y1 = p.y; Rect dragBounds = feedback.dragBounds; int w = dragBounds != null ? dragBounds.w : 0; int h = dragBounds != null ? dragBounds.h : 0; if (!GridLayoutRule.sGridMode) { if (dragBounds != null) { // Sometimes the items are centered under the mouse so // offset by the top left corner distance x1 += dragBounds.x; y1 += dragBounds.y; } int x2 = x1 + w; int y2 = y1 + h; if (x2 < bounds.x || y2 < bounds.y || x1 > bounds.x2() || y1 > bounds.y2()) { return; } List<GridMatch> columnMatches = new ArrayList<GridMatch>(); List<GridMatch> rowMatches = new ArrayList<GridMatch>(); int max = BaseLayoutRule.getMaxMatchDistance(); // Column matches: addLeftSideMatch(x1, columnMatches, max); addRightSideMatch(x2, columnMatches, max); addCenterColumnMatch(bounds, x1, y1, x2, y2, columnMatches, max); // Row matches: int row = (mGrid.getViewCount() == 0) ? 0 : mGrid.getClosestRow(y1); int rowY = mGrid.getRowY(row); addTopMatch(y1, rowMatches, max, row, rowY); addBaselineMatch(feedback.dragBaseline, y1, rowMatches, max, row, rowY); addBottomMatch(y2, rowMatches, max); // Look for gap-matches: Predefined spacing between widgets. // TODO: Make this use metadata for predefined spacing between // pairs of types of components. For example, buttons have certain // inserts in their 9-patch files (depending on the theme) that should // be considered and subtracted from the overall proposed distance! addColumnGapMatch(bounds, x1, x2, columnMatches, max); addRowGapMatch(bounds, y1, y2, rowMatches, max); // Fallback: Split existing cell. Also do snap-to-grid. if (GridLayoutRule.sSnapToGrid) { x1 = ((x1 - MARGIN_SIZE - bounds.x) / GRID_SIZE) * GRID_SIZE + MARGIN_SIZE + bounds.x; y1 = ((y1 - MARGIN_SIZE - bounds.y) / GRID_SIZE) * GRID_SIZE + MARGIN_SIZE + bounds.y; x2 = x1 + w; y2 = y1 + h; } if (columnMatches.size() == 0 && x1 >= bounds.x) { // Split the current cell since we have no matches // TODO: Decide whether it should be gravity left or right... columnMatches.add( new GridMatch( SegmentType.LEFT, 0, x1, mGrid.getColumn(x1), true /* createCell */, UNDEFINED)); } if (rowMatches.size() == 0 && y1 >= bounds.y) { rowMatches.add( new GridMatch( SegmentType.TOP, 0, y1, mGrid.getRow(y1), true /* createCell */, UNDEFINED)); } // Pick best matches Collections.sort(rowMatches); Collections.sort(columnMatches); mColumnMatch = null; mRowMatch = null; String columnDescription = null; String rowDescription = null; if (columnMatches.size() > 0) { mColumnMatch = columnMatches.get(0); columnDescription = mColumnMatch.getDisplayName(mGrid.layout); } if (rowMatches.size() > 0) { mRowMatch = rowMatches.get(0); rowDescription = mRowMatch.getDisplayName(mGrid.layout); } if (columnDescription != null && rowDescription != null) { feedback.tooltip = columnDescription + '\n' + rowDescription; } feedback.invalidTarget = mColumnMatch == null || mRowMatch == null; } else { // Find which cell we're inside. // TODO: Find out where within the cell we are, and offer to tweak the gravity // based on the position. int column = mGrid.getColumn(x1); int row = mGrid.getRow(y1); int leftDistance = mGrid.getColumnDistance(column, x1); int rightDistance = mGrid.getColumnDistance(column + 1, x1); int topDistance = mGrid.getRowDistance(row, y1); int bottomDistance = mGrid.getRowDistance(row + 1, y1); int SLOP = 2; int radius = mRule.getNewCellSize(); if (rightDistance < radius + SLOP) { column = Math.min(column + 1, mGrid.actualColumnCount); leftDistance = rightDistance; } if (bottomDistance < radius + SLOP) { row = Math.min(row + 1, mGrid.actualRowCount); topDistance = bottomDistance; } boolean createColumn = leftDistance < radius + SLOP; boolean createRow = topDistance < radius + SLOP; if (x1 >= bounds.x2()) { createColumn = true; } if (y1 >= bounds.y2()) { createRow = true; } int cellWidth = leftDistance + rightDistance; int cellHeight = topDistance + bottomDistance; SegmentType horizontalType = SegmentType.LEFT; SegmentType verticalType = SegmentType.TOP; int minDistance = 10; // Don't center or right/bottom align in tiny cells if (!createColumn && leftDistance > minDistance && dragBounds != null && dragBounds.w < cellWidth - 10) { if (rightDistance < leftDistance) { horizontalType = SegmentType.RIGHT; } int centerDistance = Math.abs(cellWidth / 2 - leftDistance); if (centerDistance < leftDistance / 2 && centerDistance < rightDistance / 2) { horizontalType = SegmentType.CENTER_HORIZONTAL; } } if (!createRow && topDistance > minDistance && dragBounds != null && dragBounds.h < cellHeight - 10) { if (bottomDistance < topDistance) { verticalType = SegmentType.BOTTOM; } int centerDistance = Math.abs(cellHeight / 2 - topDistance); if (centerDistance < topDistance / 2 && centerDistance < bottomDistance / 2) { verticalType = SegmentType.CENTER_VERTICAL; } } mColumnMatch = new GridMatch(horizontalType, 0, x1, column, createColumn, 0); mRowMatch = new GridMatch(verticalType, 0, y1, row, createRow, 0); StringBuilder description = new StringBuilder(50); String rowString = Integer.toString(mColumnMatch.cellIndex + 1); String columnString = Integer.toString(mRowMatch.cellIndex + 1); if (mRowMatch.createCell && mRowMatch.cellIndex < mGrid.actualRowCount) { description.append(String.format("Shift row %1$d down", mRowMatch.cellIndex + 1)); description.append('\n'); } if (mColumnMatch.createCell && mColumnMatch.cellIndex < mGrid.actualColumnCount) { description.append(String.format("Shift column %1$d right", mColumnMatch.cellIndex + 1)); description.append('\n'); } description.append(String.format("Insert into cell (%1$s,%2$s)", rowString, columnString)); description.append('\n'); description.append( String.format( "Align %1$s, %2$s", horizontalType.name().toLowerCase(Locale.US), verticalType.name().toLowerCase(Locale.US))); feedback.tooltip = description.toString(); } }