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.launcher2.Workspace.java

@Override
public void getHitRect(Rect outRect) {
    // We want the workspace to have the whole area of the display (it will find the correct
    // cell layout to drop to in the existing drag/drop logic.
    outRect.set(0, 0, mDisplaySize.x, mDisplaySize.y);
}

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

private static Rect getDrawableBounds(Drawable d) {
    Rect bounds = new Rect();
    d.copyBounds(bounds);/*www.j a  v  a2s.c  o  m*/
    if (bounds.width() == 0 || bounds.height() == 0) {
        bounds.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    } else {
        bounds.offsetTo(0, 0);
    }
    if (d instanceof PreloadIconDrawable) {
        int inset = -((PreloadIconDrawable) d).getOutset();
        bounds.inset(inset, inset);
    }
    return bounds;
}

From source file:eu.davidea.flexibleadapter.common.FlexibleItemDecoration.java

/**
 * @since 5.0.0-rc2//from  www  . j ava2 s .  c  o  m
 */
@Override
public void getItemOffsets(final Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
    int position = recyclerView.getChildAdapterPosition(view);
    // Skip check so on item deleted, offset is kept (only if general offset was set!)
    // if (position == RecyclerView.NO_POSITION) return;

    // Get custom Item Decoration or default
    RecyclerView.Adapter adapter = recyclerView.getAdapter();
    int itemType = adapter.getItemViewType(position);
    ItemDecoration deco = getItemDecoration(itemType);

    // No offset set, applies the general offset to this item decoration
    if (!deco.hasOffset()) {
        deco = new ItemDecoration(mOffset);
    }

    // Default values (LinearLayout)
    int spanIndex = 0;
    int spanSize = 1;
    int spanCount = 1;
    int orientation = RecyclerView.VERTICAL;

    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        spanIndex = lp.getSpanIndex();
        spanSize = lp.getSpanSize();
        GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
        spanCount = lm.getSpanCount(); // Assume that there are spanCount items in this row/column.
        orientation = lm.getOrientation();

    } else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view
                .getLayoutParams();
        spanIndex = lp.getSpanIndex();
        StaggeredGridLayoutManager lm = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();
        spanCount = lm.getSpanCount(); // Assume that there are spanCount items in this row/column.
        spanSize = lp.isFullSpan() ? spanCount : 1;
        orientation = lm.getOrientation();
    }

    boolean isFirstRowOrColumn = isFirstRowOrColumn(position, adapter, spanIndex, itemType);
    boolean isLastRowOrColumn = isLastRowOrColumn(position, adapter, spanIndex, spanCount, itemType);

    // Reset offset values
    int left = 0, top = 0, right = 0, bottom = 0;

    if (orientation == GridLayoutManager.VERTICAL) {
        int index = spanIndex;
        if (withLeftEdge)
            index = spanCount - spanIndex;
        left = deco.left * index / spanCount;

        index = (spanCount - (spanIndex + spanSize - 1) - 1);
        if (withRightEdge)
            index = spanIndex + spanSize;
        right = deco.right * index / spanCount;

        if (isFirstRowOrColumn && (withTopEdge)) {
            top = deco.top;
        }
        if (isLastRowOrColumn) {
            if (withBottomEdge) {
                bottom = deco.bottom;
            }
        } else {
            bottom = deco.bottom;
        }

    } else {
        int index = spanIndex;
        if (withTopEdge)
            index = spanCount - spanIndex;
        top = deco.top * index / spanCount;

        index = (spanCount - (spanIndex + spanSize - 1) - 1);
        if (withBottomEdge)
            index = spanIndex + spanSize;
        bottom = deco.bottom * index / spanCount;

        if (isFirstRowOrColumn && (withLeftEdge)) {
            left = deco.left;
        }
        if (isLastRowOrColumn) {
            if (withRightEdge) {
                right = deco.right;
            }
        } else {
            right = deco.right;
        }
    }

    outRect.set(left, top, right, bottom);

    applySectionGap(outRect, adapter, position, orientation);
}

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

/**
 * Draw the View v into the given Canvas.
 *
 * @param v the view to draw/*from w w  w.  j ava2s .  c  om*/
 * @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 = ((TextView) v).getCompoundDrawables()[1];
        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();
}

From source file:cc.flydev.launcher.Workspace.java

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

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView && pruneToDrawable) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
        destCanvas.translate(padding / 2, padding / 2);
        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;
            }
        } else if (v instanceof BubbleTextView) {
            final BubbleTextView tv = (BubbleTextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V
                    + tv.getLayout().getLineTop(0);
        } else if (v instanceof TextView) {
            final TextView tv = (TextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding()
                    + tv.getLayout().getLineTop(0);
        }
        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();
}

From source file:com.aidy.launcher3.ui.workspace.Workspace.java

/**
 * Draw the View v into the given Canvas.
 * //  w w w  .  ja  v  a 2 s  .  com
 * @param v
 *            the view to draw
 * @param destCanvas
 *            the canvas to draw on
 * @param padding
 *            the horizontal and vertical padding to use when drawing
 */
private void drawDragView(View v, Canvas destCanvas, int padding, boolean pruneToDrawable) {
    final Rect clipRect = mTempRect;
    v.getDrawingRect(clipRect);

    boolean textVisible = false;

    destCanvas.save();
    if (v instanceof TextView && pruneToDrawable) {
        Drawable d = ((TextView) v).getCompoundDrawables()[1];
        clipRect.set(0, 0, d.getIntrinsicWidth() + padding, d.getIntrinsicHeight() + padding);
        destCanvas.translate(padding / 2, padding / 2);
        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;
            }
        } else if (v instanceof BubbleTextView) {
            final BubbleTextView tv = (BubbleTextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - (int) BubbleTextView.PADDING_V
                    + tv.getLayout().getLineTop(0);
        } else if (v instanceof TextView) {
            final TextView tv = (TextView) v;
            clipRect.bottom = tv.getExtendedPaddingTop() - tv.getCompoundDrawablePadding()
                    + tv.getLayout().getLineTop(0);
        }
        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();
}

From source file:com.marlonjones.voidlauncher.CellLayout.java

void visualizeDropLocation(View v, DragPreviewProvider outlineProvider, int cellX, int cellY, int spanX,
        int spanY, boolean resize, DropTarget.DragObject dragObject) {
    final int oldDragCellX = mDragCell[0];
    final int oldDragCellY = mDragCell[1];

    if (outlineProvider == null || outlineProvider.gerenatedDragOutline == null) {
        return;/*  ww  w  .j  a v a2s  .  c  o m*/
    }

    Bitmap dragOutline = outlineProvider.gerenatedDragOutline;
    if (cellX != oldDragCellX || cellY != oldDragCellY) {
        Point dragOffset = dragObject.dragView.getDragVisualizeOffset();
        Rect dragRegion = dragObject.dragView.getDragRegion();

        mDragCell[0] = cellX;
        mDragCell[1] = cellY;

        final int oldIndex = mDragOutlineCurrent;
        mDragOutlineAnims[oldIndex].animateOut();
        mDragOutlineCurrent = (oldIndex + 1) % mDragOutlines.length;
        Rect r = mDragOutlines[mDragOutlineCurrent];

        if (resize) {
            cellToRect(cellX, cellY, spanX, spanY, r);
        } else {
            // 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;
                }
            }
            r.set(left, top, left + dragOutline.getWidth(), top + dragOutline.getHeight());
        }

        Utilities.scaleRectAboutCenter(r, getChildrenScale());
        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:com.klinker.android.launcher.launcher3.Workspace.java

/**
 * Computes the area relative to dragLayer which is used to display a page.
 */// www .java  2 s  .  c  o  m
public void getPageAreaRelativeToDragLayer(Rect outArea) {
    CellLayout child = (CellLayout) getChildAt(getNextPage());
    if (child == null) {
        return;
    }
    ShortcutAndWidgetContainer boundingLayout = child.getShortcutsAndWidgets();

    // Use the absolute left instead of the child left, as we want the visible area
    // irrespective of the visible child. Since the view can only scroll horizontally, the
    // top position is not affected.
    mTempXY[0] = getViewportOffsetX() + getPaddingLeft() + boundingLayout.getLeft();
    mTempXY[1] = child.getTop() + boundingLayout.getTop();

    float scale = mLauncher.getDragLayer().getDescendantCoordRelativeToSelf(this, mTempXY);
    outArea.set(mTempXY[0], mTempXY[1], (int) (mTempXY[0] + scale * boundingLayout.getMeasuredWidth()),
            (int) (mTempXY[1] + scale * boundingLayout.getMeasuredHeight()));
}

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

private void getDesiredAnchoredChildRectWithoutConstraints(View child, int layoutDirection, Rect anchorRect,
        Rect out, LayoutParams lp, int childWidth, int childHeight) {
    final int absGravity = GravityCompat.getAbsoluteGravity(resolveAnchoredChildGravity(lp.gravity),
            layoutDirection);/*from w  ww  .j a v a  2 s .  c  om*/
    final int absAnchorGravity = GravityCompat.getAbsoluteGravity(resolveGravity(lp.anchorGravity),
            layoutDirection);

    final int hgrav = absGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int vgrav = absGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int anchorHgrav = absAnchorGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int anchorVgrav = absAnchorGravity & Gravity.VERTICAL_GRAVITY_MASK;

    int left;
    int top;

    // Align to the anchor. This puts us in an assumed right/bottom child view gravity.
    // If this is not the case we will subtract out the appropriate portion of
    // the child size below.
    switch (anchorHgrav) {
    default:
    case Gravity.LEFT:
        left = anchorRect.left;
        break;
    case Gravity.RIGHT:
        left = anchorRect.right;
        break;
    case Gravity.CENTER_HORIZONTAL:
        left = anchorRect.left + anchorRect.width() / 2;
        break;
    }

    switch (anchorVgrav) {
    default:
    case Gravity.TOP:
        top = anchorRect.top;
        break;
    case Gravity.BOTTOM:
        top = anchorRect.bottom;
        break;
    case Gravity.CENTER_VERTICAL:
        top = anchorRect.top + anchorRect.height() / 2;
        break;
    }

    // Offset by the child view's gravity itself. The above assumed right/bottom gravity.
    switch (hgrav) {
    default:
    case Gravity.LEFT:
        left -= childWidth;
        break;
    case Gravity.RIGHT:
        // Do nothing, we're already in position.
        break;
    case Gravity.CENTER_HORIZONTAL:
        left -= childWidth / 2;
        break;
    }

    switch (vgrav) {
    default:
    case Gravity.TOP:
        top -= childHeight;
        break;
    case Gravity.BOTTOM:
        // Do nothing, we're already in position.
        break;
    case Gravity.CENTER_VERTICAL:
        top -= childHeight / 2;
        break;
    }

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

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

/**
 * Calculate the desired child rect relative to an anchor rect, respecting both
 * gravity and anchorGravity./*from  w ww .j ava 2s. co  m*/
 *
 * @param child child view to calculate a rect for
 * @param layoutDirection the desired layout direction for the CoordinatorLayout
 * @param anchorRect rect in CoordinatorLayout coordinates of the anchor view area
 * @param out rect to set to the output values
 */
void getDesiredAnchoredChildRect(View child, int layoutDirection, Rect anchorRect, Rect out) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int absGravity = GravityCompat.getAbsoluteGravity(resolveAnchoredChildGravity(lp.gravity),
            layoutDirection);
    final int absAnchorGravity = GravityCompat.getAbsoluteGravity(resolveGravity(lp.anchorGravity),
            layoutDirection);

    final int hgrav = absGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int vgrav = absGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int anchorHgrav = absAnchorGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    final int anchorVgrav = absAnchorGravity & Gravity.VERTICAL_GRAVITY_MASK;

    final int childWidth = child.getMeasuredWidth();
    final int childHeight = child.getMeasuredHeight();

    int left;
    int top;

    // Align to the anchor. This puts us in an assumed right/bottom child view gravity.
    // If this is not the case we will subtract out the appropriate portion of
    // the child size below.
    switch (anchorHgrav) {
    default:
    case Gravity.LEFT:
        left = anchorRect.left;
        break;
    case Gravity.RIGHT:
        left = anchorRect.right;
        break;
    case Gravity.CENTER_HORIZONTAL:
        left = anchorRect.left + anchorRect.width() / 2;
        break;
    }

    switch (anchorVgrav) {
    default:
    case Gravity.TOP:
        top = anchorRect.top;
        break;
    case Gravity.BOTTOM:
        top = anchorRect.bottom;
        break;
    case Gravity.CENTER_VERTICAL:
        top = anchorRect.top + anchorRect.height() / 2;
        break;
    }

    // Offset by the child view's gravity itself. The above assumed right/bottom gravity.
    switch (hgrav) {
    default:
    case Gravity.LEFT:
        left -= childWidth;
        break;
    case Gravity.RIGHT:
        // Do nothing, we're already in position.
        break;
    case Gravity.CENTER_HORIZONTAL:
        left -= childWidth / 2;
        break;
    }

    switch (vgrav) {
    default:
    case Gravity.TOP:
        top -= childHeight;
        break;
    case Gravity.BOTTOM:
        // Do nothing, we're already in position.
        break;
    case Gravity.CENTER_VERTICAL:
        top -= childHeight / 2;
        break;
    }

    final int width = getWidth();
    final int height = getHeight();

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

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