List of usage examples for android.graphics Rect set
public void set(int left, int top, int right, int bottom)
From source file:me.ccrama.redditslide.Views.SubsamplingScaleImageView.java
/** * Convert source rect to screen rect, integer values. *//*w ww . java 2 s .c o m*/ private Rect sourceToViewRect(Rect sRect, Rect vTarget) { vTarget.set((int) sourceToViewX(sRect.left), (int) sourceToViewY(sRect.top), (int) sourceToViewX(sRect.right), (int) sourceToViewY(sRect.bottom)); return vTarget; }
From source file:VerticalViewPager.java
private Rect getChildRectInPagerCoordinates(Rect outRect, View child) { if (outRect == null) { outRect = new Rect(); }//from w w w.j a v a 2 s. co m if (child == null) { outRect.set(0, 0, 0, 0); return outRect; } outRect.left = child.getLeft(); outRect.right = child.getRight(); outRect.top = child.getTop(); outRect.bottom = child.getBottom(); ViewParent parent = child.getParent(); while (parent instanceof ViewGroup && parent != this) { final ViewGroup group = (ViewGroup) parent; outRect.left += group.getLeft(); outRect.right += group.getRight(); outRect.top += group.getTop(); outRect.bottom += group.getBottom(); parent = group.getParent(); } return outRect; }
From source file:com.android.launcher3.CellLayout.java
private void getViewsIntersectingRegion(int cellX, int cellY, int spanX, int spanY, View dragView, Rect boundingRect, ArrayList<View> intersectingViews) { if (boundingRect != null) { boundingRect.set(cellX, cellY, cellX + spanX, cellY + spanY); }//from w w w.ja v a 2 s . c o m intersectingViews.clear(); Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); Rect r1 = new Rect(); final int count = mShortcutsAndWidgets.getChildCount(); for (int i = 0; i < count; i++) { View child = mShortcutsAndWidgets.getChildAt(i); if (child == dragView) continue; LayoutParams lp = (LayoutParams) child.getLayoutParams(); r1.set(lp.cellX, lp.cellY, lp.cellX + lp.cellHSpan, lp.cellY + lp.cellVSpan); if (Rect.intersects(r0, r1)) { mIntersectingViews.add(child); if (boundingRect != null) { boundingRect.union(r1); } } } }
From source file:com.android.launcher3.CellLayout.java
void visualizeDropLocation(View v, Bitmap dragOutline, int cellX, int cellY, int spanX, int spanY, boolean resize, DropTarget.DragObject dragObject) { final int oldDragCellX = mDragCell[0]; final int oldDragCellY = mDragCell[1]; if (dragOutline == null && v == null) { return;//from w w w . java2 s .c om } if (cellX != oldDragCellX || cellY != oldDragCellY) { Point dragOffset = dragObject.dragView.getDragVisualizeOffset(); Rect dragRegion = dragObject.dragView.getDragRegion(); mDragCell[0] = cellX; mDragCell[1] = cellY; // Find the top left corner of the rect the object will occupy final int[] topLeft = mTmpPoint; cellToPoint(cellX, cellY, topLeft); int left = topLeft[0]; int top = topLeft[1]; if (v != null && dragOffset == null) { // When drawing the drag outline, it did not account for margin offsets // added by the view's parent. MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); left += lp.leftMargin; top += lp.topMargin; // Offsets due to the size difference between the View and the dragOutline. // There is a size difference to account for the outer blur, which may lie // outside the bounds of the view. top += (v.getHeight() - dragOutline.getHeight()) / 2; // We center about the x axis left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) - dragOutline.getWidth()) / 2; } else { if (dragOffset != null && dragRegion != null) { // Center the drag region *horizontally* in the cell and apply a drag // outline offset left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) - dragRegion.width()) / 2; int cHeight = getShortcutsAndWidgets().getCellContentHeight(); int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f)); top += dragOffset.y + cellPaddingY; } else { // Center the drag outline in the cell left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) - dragOutline.getWidth()) / 2; top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap) - dragOutline.getHeight()) / 2; } } final int oldIndex = mDragOutlineCurrent; mDragOutlineAnims[oldIndex].animateOut(); mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; Rect r = mDragOutlines[mDragOutlineCurrent]; r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight()); if (resize) { cellToRect(cellX, cellY, spanX, spanY, r); } mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); mDragOutlineAnims[mDragOutlineCurrent].animateIn(); if (dragObject.stateAnnouncer != null) { String msg; if (isHotseat()) { msg = getContext().getString(R.string.move_to_hotseat_position, Math.max(cellX, cellY) + 1); } else { msg = getContext().getString(R.string.move_to_empty_cell, cellY + 1, cellX + 1); } dragObject.stateAnnouncer.announce(msg); } } }
From source file:android.support.design.widget.CoordinatorLayout.java
/** * Lay out a child view with no special handling. This will position the child as * if it were within a FrameLayout or similar simple frame. * * @param child child view to lay out/*from www . ja v a2s. co m*/ * @param layoutDirection ViewCompat constant for the desired layout direction */ private void layoutChild(View child, int layoutDirection) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final Rect parent = acquireTempRect(); parent.set(getPaddingLeft() + lp.leftMargin, getPaddingTop() + lp.topMargin, getWidth() - getPaddingRight() - lp.rightMargin, getHeight() - getPaddingBottom() - lp.bottomMargin); if (mLastInsets != null && ViewCompat.getFitsSystemWindows(this) && !ViewCompat.getFitsSystemWindows(child)) { // If we're set to handle insets but this child isn't, then it has been measured as // if there are no insets. We need to lay it out to match. parent.left += mLastInsets.getSystemWindowInsetLeft(); parent.top += mLastInsets.getSystemWindowInsetTop(); parent.right -= mLastInsets.getSystemWindowInsetRight(); parent.bottom -= mLastInsets.getSystemWindowInsetBottom(); } final Rect out = acquireTempRect(); GravityCompat.apply(resolveGravity(lp.gravity), child.getMeasuredWidth(), child.getMeasuredHeight(), parent, out, layoutDirection); child.layout(out.left, out.top, out.right, out.bottom); releaseTempRect(parent); releaseTempRect(out); }
From source file:com.marlonjones.voidlauncher.CellLayout.java
private boolean rearrangementExists(int cellX, int cellY, int spanX, int spanY, int[] direction, View ignoreView, ItemConfiguration solution) { // Return early if get invalid cell positions if (cellX < 0 || cellY < 0) return false; mIntersectingViews.clear();// www . j a v a 2 s.c om mOccupiedRect.set(cellX, cellY, cellX + spanX, cellY + spanY); // Mark the desired location of the view currently being dragged. if (ignoreView != null) { CellAndSpan c = solution.map.get(ignoreView); if (c != null) { c.cellX = cellX; c.cellY = cellY; } } Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY); Rect r1 = new Rect(); for (View child : solution.map.keySet()) { if (child == ignoreView) continue; CellAndSpan c = solution.map.get(child); LayoutParams lp = (LayoutParams) child.getLayoutParams(); r1.set(c.cellX, c.cellY, c.cellX + c.spanX, c.cellY + c.spanY); if (Rect.intersects(r0, r1)) { if (!lp.canReorder) { return false; } mIntersectingViews.add(child); } } solution.intersectingViews = new ArrayList<View>(mIntersectingViews); // First we try to find a solution which respects the push mechanic. That is, // we try to find a solution such that no displaced item travels through another item // without also displacing that item. if (attemptPushInDirection(mIntersectingViews, mOccupiedRect, direction, ignoreView, solution)) { return true; } // Next we try moving the views as a block, but without requiring the push mechanic. if (addViewsToTempLocation(mIntersectingViews, mOccupiedRect, direction, ignoreView, solution)) { return true; } // Ok, they couldn't move as a block, let's move them individually for (View v : mIntersectingViews) { if (!addViewToTempLocation(v, mOccupiedRect, direction, solution)) { return false; } } return true; }
From source file:com.android.leanlauncher.CellLayout.java
void visualizeDropLocation(View v, Bitmap dragOutline, int originX, int originY, int cellX, int cellY, int spanX, int spanY, boolean resize, Point dragOffset, Rect dragRegion) { final int oldDragCellX = mDragCell[0]; final int oldDragCellY = mDragCell[1]; if (dragOutline == null && v == null) { return;/* ww w . j a va2 s.c o m*/ } if (cellX != oldDragCellX || cellY != oldDragCellY) { mDragCell[0] = cellX; mDragCell[1] = cellY; // Find the top left corner of the rect the object will occupy final int[] topLeft = mTmpPoint; cellToPoint(cellX, cellY, topLeft); int left = topLeft[0]; int top = topLeft[1]; if (v != null && dragOffset == null) { // When drawing the drag outline, it did not account for margin offsets // added by the view's parent. MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams(); left += lp.leftMargin; top += lp.topMargin; // Offsets due to the size difference between the View and the dragOutline. // There is a size difference to account for the outer blur, which may lie // outside the bounds of the view. top += (v.getHeight() - dragOutline.getHeight()) / 2; // We center about the x axis left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) - dragOutline.getWidth()) / 2; } else { if (dragOffset != null && dragRegion != null) { // Center the drag region *horizontally* in the cell and apply a drag // outline offset left += dragOffset.x + ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) - dragRegion.width()) / 2; int cHeight = getShortcutsAndWidgets().getCellContentHeight(); int cellPaddingY = (int) Math.max(0, ((mCellHeight - cHeight) / 2f)); top += dragOffset.y + cellPaddingY; } else { // Center the drag outline in the cell left += ((mCellWidth * spanX) + ((spanX - 1) * mWidthGap) - dragOutline.getWidth()) / 2; top += ((mCellHeight * spanY) + ((spanY - 1) * mHeightGap) - dragOutline.getHeight()) / 2; } } final int oldIndex = mDragOutlineCurrent; mDragOutlineAnims[oldIndex].animateOut(); mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length; Rect r = mDragOutlines[mDragOutlineCurrent]; r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight()); if (resize) { cellToRect(cellX, cellY, spanX, spanY, r); } mDragOutlineAnims[mDragOutlineCurrent].setTag(dragOutline); mDragOutlineAnims[mDragOutlineCurrent].animateIn(); } }
From source file:com.android.launcher3.CellLayout.java
/** * Find a vacant area that will fit the given bounds nearest the requested * cell location. Uses Euclidean distance to score multiple vacant areas. * * @param pixelX The X location at which you want to search for a vacant area. * @param pixelY The Y location at which you want to search for a vacant area. * @param minSpanX The minimum horizontal span required * @param minSpanY The minimum vertical span required * @param spanX Horizontal span of the object. * @param spanY Vertical span of the object. * @param ignoreOccupied If true, the result can be an occupied cell * @param result Array in which to place the result, or null (in which case a new array will * be allocated)//w ww . j a v a 2s. c om * @return The X, Y cell of a vacant area that can contain this object, * nearest the requested location. */ private int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, boolean ignoreOccupied, int[] result, int[] resultSpan) { lazyInitTempRectStack(); // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds // to the center of the item, but we are searching based on the top-left cell, so // we translate the point over to correspond to the top-left. pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; // Keep track of best-scoring drop area final int[] bestXY = result != null ? result : new int[2]; double bestDistance = Double.MAX_VALUE; final Rect bestRect = new Rect(-1, -1, -1, -1); final Stack<Rect> validRegions = new Stack<Rect>(); final int countX = mCountX; final int countY = mCountY; if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) { return bestXY; } for (int y = 0; y < countY - (minSpanY - 1); y++) { inner: for (int x = 0; x < countX - (minSpanX - 1); x++) { int ySize = -1; int xSize = -1; if (ignoreOccupied) { // First, let's see if this thing fits anywhere for (int i = 0; i < minSpanX; i++) { for (int j = 0; j < minSpanY; j++) { if (mOccupied[x + i][y + j]) { continue inner; } } } xSize = minSpanX; ySize = minSpanY; // We know that the item will fit at _some_ acceptable size, now let's see // how big we can make it. We'll alternate between incrementing x and y spans // until we hit a limit. boolean incX = true; boolean hitMaxX = xSize >= spanX; boolean hitMaxY = ySize >= spanY; while (!(hitMaxX && hitMaxY)) { if (incX && !hitMaxX) { for (int j = 0; j < ySize; j++) { if (x + xSize > countX - 1 || mOccupied[x + xSize][y + j]) { // We can't move out horizontally hitMaxX = true; } } if (!hitMaxX) { xSize++; } } else if (!hitMaxY) { for (int i = 0; i < xSize; i++) { if (y + ySize > countY - 1 || mOccupied[x + i][y + ySize]) { // We can't move out vertically hitMaxY = true; } } if (!hitMaxY) { ySize++; } } hitMaxX |= xSize >= spanX; hitMaxY |= ySize >= spanY; incX = !incX; } incX = true; hitMaxX = xSize >= spanX; hitMaxY = ySize >= spanY; } final int[] cellXY = mTmpPoint; cellToCenterPoint(x, y, cellXY); // We verify that the current rect is not a sub-rect of any of our previous // candidates. In this case, the current rect is disqualified in favour of the // containing rect. Rect currentRect = mTempRectStack.pop(); currentRect.set(x, y, x + xSize, y + ySize); boolean contained = false; for (Rect r : validRegions) { if (r.contains(currentRect)) { contained = true; break; } } validRegions.push(currentRect); double distance = Math.hypot(cellXY[0] - pixelX, cellXY[1] - pixelY); if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) { bestDistance = distance; bestXY[0] = x; bestXY[1] = y; if (resultSpan != null) { resultSpan[0] = xSize; resultSpan[1] = ySize; } bestRect.set(currentRect); } } } // Return -1, -1 if no suitable location found if (bestDistance == Double.MAX_VALUE) { bestXY[0] = -1; bestXY[1] = -1; } recycleTempRects(validRegions); return bestXY; }
From source file:android.support.design.widget.CoordinatorLayout.java
private void offsetChildByInset(final View child, final Rect inset, final int layoutDirection) { if (!ViewCompat.isLaidOut(child)) { // The view has not been laid out yet, so we can't obtain its bounds. return;/*from ww w . ja v a 2 s. c o m*/ } if (child.getWidth() <= 0 || child.getHeight() <= 0) { // Bounds are empty so there is nothing to dodge against, skip... return; } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final Behavior behavior = lp.getBehavior(); final Rect dodgeRect = acquireTempRect(); final Rect bounds = acquireTempRect(); bounds.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom()); if (behavior != null && behavior.getInsetDodgeRect(this, child, dodgeRect)) { // Make sure that the rect is within the view's bounds if (!bounds.contains(dodgeRect)) { throw new IllegalArgumentException("Rect should be within the child's bounds." + " Rect:" + dodgeRect.toShortString() + " | Bounds:" + bounds.toShortString()); } } else { dodgeRect.set(bounds); } // We can release the bounds rect now releaseTempRect(bounds); if (dodgeRect.isEmpty()) { // Rect is empty so there is nothing to dodge against, skip... releaseTempRect(dodgeRect); return; } final int absDodgeInsetEdges = GravityCompat.getAbsoluteGravity(lp.dodgeInsetEdges, layoutDirection); boolean offsetY = false; if ((absDodgeInsetEdges & Gravity.TOP) == Gravity.TOP) { int distance = dodgeRect.top - lp.topMargin - lp.mInsetOffsetY; if (distance < inset.top) { setInsetOffsetY(child, inset.top - distance); offsetY = true; } } if ((absDodgeInsetEdges & Gravity.BOTTOM) == Gravity.BOTTOM) { int distance = getHeight() - dodgeRect.bottom - lp.bottomMargin + lp.mInsetOffsetY; if (distance < inset.bottom) { setInsetOffsetY(child, distance - inset.bottom); offsetY = true; } } if (!offsetY) { setInsetOffsetY(child, 0); } boolean offsetX = false; if ((absDodgeInsetEdges & Gravity.LEFT) == Gravity.LEFT) { int distance = dodgeRect.left - lp.leftMargin - lp.mInsetOffsetX; if (distance < inset.left) { setInsetOffsetX(child, inset.left - distance); offsetX = true; } } if ((absDodgeInsetEdges & Gravity.RIGHT) == Gravity.RIGHT) { int distance = getWidth() - dodgeRect.right - lp.rightMargin + lp.mInsetOffsetX; if (distance < inset.right) { setInsetOffsetX(child, distance - inset.right); offsetX = true; } } if (!offsetX) { setInsetOffsetX(child, 0); } releaseTempRect(dodgeRect); }
From source file:com.android.leanlauncher.CellLayout.java
/** * Find a vacant area that will fit the given bounds nearest the requested * cell location. Uses Euclidean distance to score multiple vacant areas. * * @param pixelX The X location at which you want to search for a vacant area. * @param pixelY The Y location at which you want to search for a vacant area. * @param minSpanX The minimum horizontal span required * @param minSpanY The minimum vertical span required * @param spanX Horizontal span of the object. * @param spanY Vertical span of the object. * @param ignoreOccupied If true, the result can be an occupied cell * @param result Array in which to place the result, or null (in which case a new array will * be allocated)//ww w. j av a 2s . c o m * @return The X, Y cell of a vacant area that can contain this object, * nearest the requested location. */ int[] findNearestArea(int pixelX, int pixelY, int minSpanX, int minSpanY, int spanX, int spanY, View ignoreView, boolean ignoreOccupied, int[] result, int[] resultSpan, boolean[][] occupied) { lazyInitTempRectStack(); // mark space take by ignoreView as available (method checks if ignoreView is null) markCellsAsUnoccupiedForView(ignoreView, occupied); // For items with a spanX / spanY > 1, the passed in point (pixelX, pixelY) corresponds // to the center of the item, but we are searching based on the top-left cell, so // we translate the point over to correspond to the top-left. pixelX -= (mCellWidth + mWidthGap) * (spanX - 1) / 2f; pixelY -= (mCellHeight + mHeightGap) * (spanY - 1) / 2f; // Keep track of best-scoring drop area final int[] bestXY = result != null ? result : new int[2]; double bestDistance = Double.MAX_VALUE; final Rect bestRect = new Rect(-1, -1, -1, -1); final Stack<Rect> validRegions = new Stack<Rect>(); final int countX = mCountX; final int countY = mCountY; if (minSpanX <= 0 || minSpanY <= 0 || spanX <= 0 || spanY <= 0 || spanX < minSpanX || spanY < minSpanY) { return bestXY; } for (int y = 0; y < countY - (minSpanY - 1); y++) { inner: for (int x = 0; x < countX - (minSpanX - 1); x++) { int ySize = -1; int xSize = -1; if (ignoreOccupied) { // First, let's see if this thing fits anywhere for (int i = 0; i < minSpanX; i++) { for (int j = 0; j < minSpanY; j++) { if (occupied[x + i][y + j]) { continue inner; } } } xSize = minSpanX; ySize = minSpanY; // We know that the item will fit at _some_ acceptable size, now let's see // how big we can make it. We'll alternate between incrementing x and y spans // until we hit a limit. boolean incX = true; boolean hitMaxX = xSize >= spanX; boolean hitMaxY = ySize >= spanY; while (!(hitMaxX && hitMaxY)) { if (incX && !hitMaxX) { for (int j = 0; j < ySize; j++) { if (x + xSize > countX - 1 || occupied[x + xSize][y + j]) { // We can't move out horizontally hitMaxX = true; } } if (!hitMaxX) { xSize++; } } else if (!hitMaxY) { for (int i = 0; i < xSize; i++) { if (y + ySize > countY - 1 || occupied[x + i][y + ySize]) { // We can't move out vertically hitMaxY = true; } } if (!hitMaxY) { ySize++; } } hitMaxX |= xSize >= spanX; hitMaxY |= ySize >= spanY; incX = !incX; } incX = true; hitMaxX = xSize >= spanX; hitMaxY = ySize >= spanY; } final int[] cellXY = mTmpXY; cellToCenterPoint(x, y, cellXY); // We verify that the current rect is not a sub-rect of any of our previous // candidates. In this case, the current rect is disqualified in favour of the // containing rect. Rect currentRect = mTempRectStack.pop(); currentRect.set(x, y, x + xSize, y + ySize); boolean contained = false; for (Rect r : validRegions) { if (r.contains(currentRect)) { contained = true; break; } } validRegions.push(currentRect); double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) + Math.pow(cellXY[1] - pixelY, 2)); if ((distance <= bestDistance && !contained) || currentRect.contains(bestRect)) { bestDistance = distance; bestXY[0] = x; bestXY[1] = y; if (resultSpan != null) { resultSpan[0] = xSize; resultSpan[1] = ySize; } bestRect.set(currentRect); } } } // re-mark space taken by ignoreView as occupied markCellsAsOccupiedForView(ignoreView, occupied); // Return -1, -1 if no suitable location found if (bestDistance == Double.MAX_VALUE) { bestXY[0] = -1; bestXY[1] = -1; } recycleTempRects(validRegions); return bestXY; }