Example usage for android.graphics Rect set

List of usage examples for android.graphics Rect set

Introduction

In this page you can find the example usage for android.graphics Rect set.

Prototype

public void set(int left, int top, int right, int bottom) 

Source Link

Document

Set the rectangle's coordinates to the specified values.

Usage

From source file:com.android.launcher3.CellLayout.java

/**
* Given a cell coordinate and span fills out a corresponding pixel rect
*
* @param cellX X coordinate of the cell//ww w.jav a2s .  c om
* @param cellY Y coordinate of the cell
* @param result Rect in which to write the result
*/
void regionToRect(int cellX, int cellY, int spanX, int spanY, Rect result) {
    final int hStartPadding = getPaddingLeft();
    final int vStartPadding = getPaddingTop();
    final int left = hStartPadding + cellX * (mCellWidth + mWidthGap);
    final int top = vStartPadding + cellY * (mCellHeight + mHeightGap);
    result.set(left, top, left + (spanX * mCellWidth + (spanX - 1) * mWidthGap),
            top + (spanY * mCellHeight + (spanY - 1) * mHeightGap));
}

From source file:com.android.launcher3.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();//from   w w  w .  j  a v a2 s .  c o  m
    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.x = cellX;
            c.y = 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.x, c.y, c.x + c.spanX, c.y + 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:android.support.design.widget.CoordinatorLayout.java

/**
 * Get the position rect for the given child. If the child has currently requested layout
 * or has a visibility of GONE./*from   ww  w  . ja  va 2  s.co  m*/
 *
 * @param child child view to check
 * @param transform true to include transformation in the output rect, false to
 *                        only account for the base position
 * @param out rect to set to the output values
 */
void getChildRect(View child, boolean transform, Rect out) {
    if (child.isLayoutRequested() || child.getVisibility() == View.GONE) {
        out.setEmpty();
        return;
    }
    if (transform) {
        getDescendantRect(child, out);
    } else {
        out.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
    }
}

From source file:org.chromium.chrome.browser.toolbar.ToolbarPhone.java

/**
 * Calculate the bounds for the location bar background and set them to {@code out}.
 *///from   ww w . j  a va 2 s .c o  m
private void updateLocationBarBackgroundBounds(Rect out, VisualState visualState) {
    // Calculate the visible boundaries of the left and right most child views of the
    // location bar.
    float expansion = visualState == VisualState.NEW_TAB_NORMAL ? 1 : mUrlExpansionPercent;
    int leftViewPosition = (int) MathUtils.interpolate(getViewBoundsLeftOfLocationBar(visualState),
            -mLocationBarBackgroundCornerRadius, expansion);
    int rightViewPosition = (int) MathUtils.interpolate(getViewBoundsRightOfLocationBar(visualState),
            getWidth() + mLocationBarBackgroundCornerRadius, expansion);

    // The bounds are set by the following:
    // - The left most visible location bar child view.
    // - The top of the viewport is aligned with the top of the location bar.
    // - The right most visible location bar child view.
    // - The bottom of the viewport is aligned with the bottom of the location bar.
    // Additional padding can be applied for use during animations.
    int verticalMargin = (int) MathUtils.interpolate(mLocationBarVerticalMargin, 0, expansion);
    out.set(leftViewPosition, mLocationBar.getTop() + verticalMargin, rightViewPosition,
            mLocationBar.getBottom() - verticalMargin);
}

From source file:com.android.leanlauncher.CellLayout.java

Rect getContentRect(Rect r) {
    if (r == null) {
        r = new Rect();
    }/*from  w  w  w . j av  a  2s  .  c  o  m*/
    int left = getPaddingLeft();
    int top = getPaddingTop();
    int right = left + getWidth() - getPaddingLeft() - getPaddingRight();
    int bottom = top + getHeight() - getPaddingTop() - getPaddingBottom();
    r.set(left, top, right, bottom);
    return r;
}

From source file:example.luojing.androidsourceanalysis.ViewPager.java

/**
 * ViewPager????View??//from   www .  j  a  va  2 s.co  m
 */
private Rect getChildRectInPagerCoordinates(Rect outRect, View child) {
    if (outRect == null) {
        outRect = new Rect();
    }
    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.huyn.demogroup.bahavior.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   w  w  w . j ava2 s . c  o  m*/
    }

    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int absDodgeInsetEdges = GravityCompat.getAbsoluteGravity(lp.dodgeInsetEdges, layoutDirection);

    final Behavior behavior = lp.getBehavior();
    final Rect rect = mTempRect3;
    if (behavior != null && behavior.getInsetDodgeRect(this, child, rect)) {
        // Make sure that it intersects the views bounds
        if (!rect.intersect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom())) {
            throw new IllegalArgumentException("Rect should intersect with child's bounds.");
        }
    } else {
        rect.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
    }

    if (rect.isEmpty()) {
        // Rect is empty so there is nothing to dodge against, skip...
        return;
    }

    boolean offsetY = false;
    if ((absDodgeInsetEdges & Gravity.TOP) == Gravity.TOP) {
        int distance = rect.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() - rect.bottom - lp.bottomMargin + lp.mInsetOffsetY;
        if (distance < inset.bottom) {
            setInsetOffsetY(child, distance - inset.bottom);
            offsetY = true;
        }
    }
    if (!offsetY) {
        SysoutUtil.sysout("CoordinatorLayout", "offsetChildByInset");
        setInsetOffsetY(child, 0);
    }

    boolean offsetX = false;
    if ((absDodgeInsetEdges & Gravity.LEFT) == Gravity.LEFT) {
        int distance = rect.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() - rect.right - lp.rightMargin + lp.mInsetOffsetX;
        if (distance < inset.right) {
            setInsetOffsetX(child, distance - inset.right);
            offsetX = true;
        }
    }
    if (!offsetX) {
        setInsetOffsetX(child, 0);
    }
}

From source file:android.support.design.widget.CoordinatorLayout.java

private void constrainChildRect(LayoutParams lp, Rect out, int childWidth, int childHeight) {
    final int width = getWidth();
    final int height = getHeight();

    // Obey margins and padding
    int left = Math.max(getPaddingLeft() + lp.leftMargin,
            Math.min(out.left, width - getPaddingRight() - childWidth - lp.rightMargin));
    int top = Math.max(getPaddingTop() + lp.topMargin,
            Math.min(out.top, height - getPaddingBottom() - childHeight - lp.bottomMargin));

    out.set(left, top, left + childWidth, top + childHeight);
}

From source file:com.android.launcher3.CellLayout.java

/**
 * Computes a bounding rectangle for a range of cells
 *
 * @param cellX X coordinate of upper left corner expressed as a cell position
 * @param cellY Y coordinate of upper left corner expressed as a cell position
 * @param cellHSpan Width in cells//from w ww  .j  a v  a 2  s  . c o m
 * @param cellVSpan Height in cells
 * @param resultRect Rect into which to put the results
 */
public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, Rect resultRect) {
    final int cellWidth = mCellWidth;
    final int cellHeight = mCellHeight;
    final int widthGap = mWidthGap;
    final int heightGap = mHeightGap;

    final int hStartPadding = getPaddingLeft();
    final int vStartPadding = getPaddingTop();

    int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
    int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);

    int x = hStartPadding + cellX * (cellWidth + widthGap);
    int y = vStartPadding + cellY * (cellHeight + heightGap);

    resultRect.set(x, y, x + width, y + height);
}

From source file:com.klinker.android.launcher.launcher3.Workspace.java

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw/*  w ww.j  av  a  2s.co m*/
 * @param destCanvas the canvas to draw on
 * @param padding the horizontal and vertical padding to use when drawing
 */
private static void drawDragView(View v, Canvas destCanvas, int padding) {
    final Rect clipRect = sTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView) {
        Drawable d = getTextViewIcon((TextView) v);
        Rect bounds = getDrawableBounds(d);
        clipRect.set(0, 0, bounds.width() + padding, bounds.height() + padding);
        destCanvas.translate(padding / 2 - bounds.left, padding / 2 - bounds.top);
        d.draw(destCanvas);
    } else {
        if (v instanceof FolderIcon) {
            // For FolderIcons the text can bleed into the icon area, and so we need to
            // hide the text completely (which can't be achieved by clipping).
            if (((FolderIcon) v).getTextVisible()) {
                ((FolderIcon) v).setTextVisible(false);
                textVisible = true;
            }
        }
        destCanvas.translate(-v.getScrollX() + padding / 2, -v.getScrollY() + padding / 2);
        destCanvas.clipRect(clipRect, Op.REPLACE);
        v.draw(destCanvas);

        // Restore text visibility of FolderIcon if necessary
        if (textVisible) {
            ((FolderIcon) v).setTextVisible(true);
        }
    }
    destCanvas.restore();
}