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:android.support.v7.widget.AbstractXpListPopupWindow.java

private void getBackgroundPadding(Rect out) {
    Drawable background = mPopup.getBackground();
    if (background != null) {
        background.getPadding(out);// w  w  w  .j  ava 2 s .  com
    } else {
        out.set(0, 0, 0, 0);
    }
}

From source file:com.android.launcher2.AsyncTaskCallback.java

private Bitmap getWidgetPreview(ComponentName provider, int previewImage, int iconId, int cellHSpan,
        int cellVSpan, int maxWidth, int maxHeight) {
    // Load the preview image if possible
    String packageName = provider.getPackageName();
    if (maxWidth < 0)
        maxWidth = Integer.MAX_VALUE;
    if (maxHeight < 0)
        maxHeight = Integer.MAX_VALUE;

    Drawable drawable = null;//from w w w.j a  v a2s.co m
    if (previewImage != 0) {
        drawable = mPackageManager.getDrawable(packageName, previewImage, null);
        if (drawable == null) {
            Log.w(TAG, "Can't load widget preview drawable 0x" + Integer.toHexString(previewImage)
                    + " for provider: " + provider);
        }
    }

    int bitmapWidth;
    int bitmapHeight;
    Bitmap defaultPreview = null;
    boolean widgetPreviewExists = (drawable != null);
    if (widgetPreviewExists) {
        bitmapWidth = drawable.getIntrinsicWidth();
        bitmapHeight = drawable.getIntrinsicHeight();
    } else {
        // Generate a preview image if we couldn't load one
        if (cellHSpan < 1)
            cellHSpan = 1;
        if (cellVSpan < 1)
            cellVSpan = 1;

        BitmapDrawable previewDrawable = (BitmapDrawable) getResources()
                .getDrawable(R.drawable.widget_preview_tile);
        final int previewDrawableWidth = previewDrawable.getIntrinsicWidth();
        final int previewDrawableHeight = previewDrawable.getIntrinsicHeight();
        bitmapWidth = previewDrawableWidth * cellHSpan; // subtract 2 dips
        bitmapHeight = previewDrawableHeight * cellVSpan;

        defaultPreview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        c.setBitmap(defaultPreview);
        previewDrawable.setBounds(0, 0, bitmapWidth, bitmapHeight);
        previewDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        previewDrawable.draw(c);
        c.setBitmap(null);

        // Draw the icon in the top left corner
        int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage);
        int smallestSide = Math.min(bitmapWidth, bitmapHeight);
        float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);

        try {
            Drawable icon = null;
            int hoffset = (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
            int yoffset = (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
            if (iconId > 0)
                icon = mIconCache.getFullResIcon(packageName, iconId);
            if (icon != null) {
                renderDrawableToBitmap(icon, defaultPreview, hoffset, yoffset, (int) (mAppIconSize * iconScale),
                        (int) (mAppIconSize * iconScale));
            }
        } catch (Resources.NotFoundException e) {
        }
    }

    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    float scale = 1f;
    if (bitmapWidth > maxWidth) {
        scale = maxWidth / (float) bitmapWidth;
    }
    if (scale != 1f) {
        bitmapWidth = (int) (scale * bitmapWidth);
        bitmapHeight = (int) (scale * bitmapHeight);
    }

    Bitmap preview = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888);

    // Draw the scaled preview into the final bitmap
    if (widgetPreviewExists) {
        renderDrawableToBitmap(drawable, preview, 0, 0, bitmapWidth, bitmapHeight);
    } else {
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        final Rect src = mCachedAppWidgetPreviewSrcRect.get();
        final Rect dest = mCachedAppWidgetPreviewDestRect.get();
        c.setBitmap(preview);
        src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
        dest.set(0, 0, preview.getWidth(), preview.getHeight());

        Paint p = mCachedAppWidgetPreviewPaint.get();
        if (p == null) {
            p = new Paint();
            p.setFilterBitmap(true);
            mCachedAppWidgetPreviewPaint.set(p);
        }
        c.drawBitmap(defaultPreview, src, dest, p);
        c.setBitmap(null);
    }
    return preview;
}

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

/** Return a rect that has the cellWidth/cellHeight (left, top), and
 * widthGap/heightGap (right, bottom) */
static void getMetrics(Rect metrics, int paddedMeasureWidth, int paddedMeasureHeight, int countX, int countY) {
    LauncherAppState app = LauncherAppState.getInstance();
    DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
    metrics.set(grid.calculateCellWidth(paddedMeasureWidth, countX),
            grid.calculateCellHeight(paddedMeasureHeight, countY), 0, 0);
}

From source file:com.fondesa.recyclerviewdivider.RecyclerViewDivider.java

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    final int listSize = parent.getAdapter().getItemCount();
    if (listSize <= 0)
        return;/*from w  w w  . j  a  v  a2  s . co m*/

    int itemPosition = parent.getChildAdapterPosition(view);
    final int groupIndex = RecyclerViewDividerUtils.getGroupIndex(parent, itemPosition);
    final int groupCount = RecyclerViewDividerUtils.getGroupCount(parent, listSize);

    @VisibilityFactory.Show
    int showDivider = mVisibilityFactory.displayDividerForItem(groupCount, groupIndex);
    if (showDivider == VisibilityFactory.SHOW_NONE)
        return;

    final int orientation = RecyclerViewDividerUtils.getOrientation(parent);
    final int spanCount = RecyclerViewDividerUtils.getSpanCount(parent);
    final int spanSize = RecyclerViewDividerUtils.getSpanSize(parent, itemPosition);

    int lineAccumulatedSpan = RecyclerViewDividerUtils.getAccumulatedSpanInLine(parent, spanSize, itemPosition,
            groupIndex);

    final Drawable divider = mDrawableFactory.drawableForItem(groupCount, groupIndex);
    int size = mSizeFactory.sizeForItem(divider, orientation, groupCount, groupIndex);
    int marginSize = mMarginFactory.marginSizeForItem(groupCount, groupIndex);

    int halfSize = size / 2 + marginSize;

    size = showDivider == VisibilityFactory.SHOW_ITEMS_ONLY ? 0 : size;
    halfSize = showDivider == VisibilityFactory.SHOW_GROUP_ONLY ? 0 : halfSize;

    if (orientation == RecyclerView.VERTICAL) {
        if (spanCount == 1 || spanSize == spanCount) {
            // LinearLayoutManager or GridLayoutManager with 1 column
            outRect.set(0, 0, 0, size);
        } else if (lineAccumulatedSpan == spanSize) {
            // first element in the group
            outRect.set(0, 0, halfSize, size);
        } else if (lineAccumulatedSpan == spanCount) {
            // last element in the group
            outRect.set(halfSize, 0, 0, size);
        } else {
            // element in the middle
            outRect.set(halfSize, 0, halfSize, size);
        }
    } else {
        if (spanCount == 1 || spanSize == spanCount) {
            // LinearLayoutManager or GridLayoutManager with 1 row
            outRect.set(0, 0, size, 0);
        } else if (lineAccumulatedSpan == spanSize) {
            // first element in the group
            outRect.set(0, 0, size, halfSize);
        } else if (lineAccumulatedSpan == spanCount) {
            // last element in the group
            outRect.set(0, halfSize, size, 0);
        } else {
            // element in the middle
            outRect.set(0, halfSize, size, halfSize);
        }
    }
}

From source file:android.support.designox.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//w  w  w  . j  av  a 2 s. c  o 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 = mTempRect1;
    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 = mTempRect2;
    GravityCompat.apply(resolveGravity(lp.gravity), child.getMeasuredWidth(), child.getMeasuredHeight(), parent,
            out, layoutDirection);
    child.layout(out.left, out.top, out.right, out.bottom);
}

From source file:chan.android.app.bitwise.util.StaggeredGridView.java

void positionSelector(int position, View sel) {
    if (position != INVALID_POSITION) {
        mSelectorPosition = position;//from  ww w. j a v  a2 s  . c o  m
    }

    final Rect selectorRect = mSelectorRect;
    selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());
    if (sel instanceof SelectionBoundsAdjuster) {
        ((SelectionBoundsAdjuster) sel).adjustListItemSelectionBounds(selectorRect);
    }

    positionSelector(selectorRect.left, selectorRect.top, selectorRect.right, selectorRect.bottom);

    final boolean isChildViewEnabled = mIsChildViewEnabled;
    if (sel.isEnabled() != isChildViewEnabled) {
        mIsChildViewEnabled = !isChildViewEnabled;
        if (getSelectedItemPosition() != INVALID_POSITION) {
            refreshDrawableState();
        }
    }
}

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

/**
 * Get the position rect for the given child. If the child has currently requested layout
 * or has a visibility of GONE./*  w w w . j a v  a2 s.  c  om*/
 *
 * @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.set(0, 0, 0, 0);
        return;
    }
    if (transform) {
        getDescendantRect(child, out);
    } else {
        out.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
    }
}

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * @param out Margins relative to left, top, right and bottom of the window.
 */// ww  w  . ja v  a  2  s  . c o  m
private void getBoundsInWindow(Rect out) {
    final View bounds = mDropDownBoundsView;
    if (bounds != null) {
        bounds.getWindowVisibleDisplayFrame(mTempRect);
        final int windowTop = mTempRect.top;
        final int windowRight = mTempRect.right;
        final int windowLeft = mTempRect.left;
        final int windowBottom = mTempRect.bottom;

        bounds.getLocationInWindow(mTempLocation);
        final int boundsTop = mTempLocation[1];
        final int boundsLeft = mTempLocation[0];

        final int boundsHeight = bounds.getHeight();
        final int boundsWidth = bounds.getWidth();

        out.top = boundsTop - windowTop;
        out.left = boundsLeft - windowLeft;
        out.bottom = windowBottom - (boundsTop + boundsHeight);
        out.right = windowRight - (boundsLeft + boundsWidth);
        return;
    }

    out.set(0, 0, 0, 0);
}

From source file:com.isapp.android.circularviewpager.CircularViewPager.java

private Rect getChildRectInPagerCoordinates(Rect outRect, View child) {
    if (outRect == null) {
        outRect = new Rect();
    }/*ww  w .  ja va 2s  .  c  o  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.marlonjones.voidlauncher.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 w  w  .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.cells[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.cells[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.cells[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;
}