Example usage for android.graphics Rect offset

List of usage examples for android.graphics Rect offset

Introduction

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

Prototype

public void offset(int dx, int dy) 

Source Link

Document

Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.

Usage

From source file:com.apptentive.android.sdk.view.ApptentiveNestedScrollView.java

@Override
public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
    // offset into coordinate space of this scroll view
    rectangle.offset(child.getLeft() - child.getScrollX(), child.getTop() - child.getScrollY());

    return scrollToChildRect(rectangle, immediate);
}

From source file:com.android.launcher3.folder.FolderIcon.java

private void onDrop(final ShortcutInfo item, DragView animateView, Rect finalRect,
        float scaleRelativeToDragLayer, int index, Runnable postAnimationRunnable) {
    item.cellX = -1;//from w  w w. j  a  v  a  2 s .c om
    item.cellY = -1;

    // Typically, the animateView corresponds to the DragView; however, if this is being done
    // after a configuration activity (ie. for a Shortcut being dragged from AllApps) we
    // will not have a view to animate
    if (animateView != null) {
        DragLayer dragLayer = mLauncher.getDragLayer();
        Rect from = new Rect();
        dragLayer.getViewRectRelativeToSelf(animateView, from);
        Rect to = finalRect;
        if (to == null) {
            to = new Rect();
            Workspace workspace = mLauncher.getWorkspace();
            // Set cellLayout and this to it's final state to compute final animation locations
            workspace.setFinalTransitionTransform((CellLayout) getParent().getParent());
            float scaleX = getScaleX();
            float scaleY = getScaleY();
            setScaleX(1.0f);
            setScaleY(1.0f);
            scaleRelativeToDragLayer = dragLayer.getDescendantRectRelativeToSelf(this, to);
            // Finished computing final animation locations, restore current state
            setScaleX(scaleX);
            setScaleY(scaleY);
            workspace.resetTransitionTransform((CellLayout) getParent().getParent());
        }

        int[] center = new int[2];
        float scale = getLocalCenterForIndex(index, index + 1, center);
        center[0] = (int) Math.round(scaleRelativeToDragLayer * center[0]);
        center[1] = (int) Math.round(scaleRelativeToDragLayer * center[1]);

        to.offset(center[0] - animateView.getMeasuredWidth() / 2,
                center[1] - animateView.getMeasuredHeight() / 2);

        float finalAlpha = index < mPreviewLayoutRule.numItems() ? 0.5f : 0f;

        float finalScale = scale * scaleRelativeToDragLayer;
        dragLayer.animateView(animateView, from, to, finalAlpha, 1, 1, finalScale, finalScale,
                DROP_IN_ANIMATION_DURATION, new DecelerateInterpolator(2), new AccelerateInterpolator(2),
                postAnimationRunnable, DragLayer.ANIMATION_END_DISAPPEAR, null);
        addItem(item);
        mFolder.hideItem(item);

        final PreviewItemDrawingParams params = index < mDrawingParams.size() ? mDrawingParams.get(index)
                : null;
        if (params != null)
            params.hidden = true;
        postDelayed(new Runnable() {
            public void run() {
                if (params != null)
                    params.hidden = false;
                mFolder.showItem(item);
                invalidate();
            }
        }, DROP_IN_ANIMATION_DURATION);
    } else {
        addItem(item);
    }
}

From source file:org.mozilla.gecko.gfx.GeckoSoftwareLayerClient.java

public Rect beginDrawing(int width, int height, int tileWidth, int tileHeight, String metadata,
        boolean hasDirectTexture) {
    setHasDirectTexture(hasDirectTexture);

    // Make sure the tile-size matches. If it doesn't, we could crash trying
    // to access invalid memory.
    if (mHasDirectTexture) {
        if (tileWidth != 0 || tileHeight != 0) {
            Log.e(LOGTAG, "Aborting draw, incorrect tile size of " + tileWidth + "x" + tileHeight);
            return null;
        }/*from w  ww . ja va  2 s  .  c  o  m*/
    } else {
        if (tileWidth != TILE_SIZE.width || tileHeight != TILE_SIZE.height) {
            Log.e(LOGTAG, "Aborting draw, incorrect tile size of " + tileWidth + "x" + tileHeight);
            return null;
        }
    }

    LayerController controller = getLayerController();

    try {
        JSONObject viewportObject = new JSONObject(metadata);
        mNewGeckoViewport = new ViewportMetrics(viewportObject);

        // Update the background color, if it's present.
        String backgroundColorString = viewportObject.optString("backgroundColor");
        if (backgroundColorString != null) {
            controller.setCheckerboardColor(parseColorFromGecko(backgroundColorString));
        }
    } catch (JSONException e) {
        Log.e(LOGTAG, "Aborting draw, bad viewport description: " + metadata);
        return null;
    }

    // Make sure we don't spend time painting areas we aren't interested in.
    // Only do this if the Gecko viewport isn't going to override our viewport.
    Rect bufferRect = new Rect(0, 0, width, height);

    if (!mUpdateViewportOnEndDraw) {
        // First, find out our ideal displayport. We do this by taking the
        // clamped viewport origin and taking away the optimum viewport offset.
        // This would be what we would send to Gecko if adjustViewport were
        // called now.
        ViewportMetrics currentMetrics = controller.getViewportMetrics();
        PointF currentBestOrigin = RectUtils.getOrigin(currentMetrics.getClampedViewport());
        PointF viewportOffset = currentMetrics.getOptimumViewportOffset(new IntSize(width, height));
        currentBestOrigin.offset(-viewportOffset.x, -viewportOffset.y);

        Rect currentRect = RectUtils.round(new RectF(currentBestOrigin.x, currentBestOrigin.y,
                currentBestOrigin.x + width, currentBestOrigin.y + height));

        // Second, store Gecko's displayport.
        PointF currentOrigin = mNewGeckoViewport.getDisplayportOrigin();
        bufferRect = RectUtils.round(
                new RectF(currentOrigin.x, currentOrigin.y, currentOrigin.x + width, currentOrigin.y + height));

        // Take the intersection of the two as the area we're interested in rendering.
        if (!bufferRect.intersect(currentRect)) {
            // If there's no intersection, we have no need to render anything,
            // but make sure to update the viewport size.
            beginTransaction(mTileLayer);
            try {
                updateViewport(true);
            } finally {
                endTransaction(mTileLayer);
            }
            return null;
        }
        bufferRect.offset(Math.round(-currentOrigin.x), Math.round(-currentOrigin.y));
    }

    beginTransaction(mTileLayer);

    // Synchronise the buffer size with Gecko.
    if (mBufferSize.width != width || mBufferSize.height != height) {
        mBufferSize = new IntSize(width, height);

        // Reallocate the buffer if necessary
        if (mTileLayer instanceof MultiTileLayer) {
            int bpp = CairoUtils.bitsPerPixelForCairoFormat(mFormat) / 8;
            int size = mBufferSize.getArea() * bpp;
            if (mBuffer == null || mBuffer.capacity() != size) {
                // Free the old buffer
                if (mBuffer != null) {
                    GeckoAppShell.freeDirectBuffer(mBuffer);
                    mBuffer = null;
                }

                mBuffer = GeckoAppShell.allocateDirectBuffer(size);
            }
        }
    }

    return bufferRect;
}

From source file:org.chromium.chrome.browser.ntp.NewTabPageView.java

/**
 * Get the bounds of the search box in relation to the top level NewTabPage view.
 *
 * @param bounds The current drawing location of the search box.
 * @param translation The translation applied to the search box by the parent view hierarchy up
 *                    to the NewTabPage view.
 *///from w ww .  j  a va2  s . co  m
void getSearchBoxBounds(Rect bounds, Point translation) {
    int searchBoxX = (int) mSearchBoxView.getX();
    int searchBoxY = (int) mSearchBoxView.getY();

    bounds.set(searchBoxX + mSearchBoxView.getPaddingLeft(), searchBoxY + mSearchBoxView.getPaddingTop(),
            searchBoxX + mSearchBoxView.getWidth() - mSearchBoxView.getPaddingRight(),
            searchBoxY + mSearchBoxView.getHeight() - mSearchBoxView.getPaddingBottom());

    translation.set(0, 0);

    View view = mSearchBoxView;
    while (true) {
        view = (View) view.getParent();
        if (view == null) {
            // The |mSearchBoxView| is not a child of this view. This can happen if the
            // RecyclerView detaches the NewTabPageLayout after it has been scrolled out of
            // view. Set the translation to the minimum Y value as an approximation.
            translation.y = Integer.MIN_VALUE;
            break;
        }
        translation.offset(-view.getScrollX(), -view.getScrollY());
        if (view == this)
            break;
        translation.offset((int) view.getX(), (int) view.getY());
    }
    bounds.offset(translation.x, translation.y);
}

From source file:com.doubleTwist.drawerlib.ADrawerLayout.java

protected void layoutView(View v, int l, int t, int r, int b) {
    LayoutParams params = (LayoutParams) v.getLayoutParams();

    Rect bounds = new Rect();
    Rect boundsWithoutPeek = new Rect();
    int gravity = params.gravity;
    switch (gravity) {
    case Gravity.RIGHT:
        if (DEBUG)
            Log.d(TAG, "gravity: right");
        bounds.left = r - v.getMeasuredWidth() - mPeekSize.right;
        bounds.top = t;/*from www. ja  v  a  2  s  . c o  m*/
        bounds.right = r - mPeekSize.right;
        bounds.bottom = t + v.getMeasuredHeight();
        v.layout(bounds.left, bounds.top, bounds.right, bounds.bottom);
        boundsWithoutPeek = new Rect(bounds);
        boundsWithoutPeek.offset(mPeekSize.right, 0);
        mMinScrollX = -bounds.width();
        break;
    case Gravity.TOP:
        if (DEBUG)
            Log.d(TAG, "gravity: top");
        bounds.left = l;
        bounds.top = t + mPeekSize.top;
        bounds.right = v.getMeasuredWidth();
        bounds.bottom = t + v.getMeasuredHeight() + mPeekSize.top;
        v.layout(bounds.left, bounds.top, bounds.right, bounds.bottom);
        boundsWithoutPeek = new Rect(bounds);
        boundsWithoutPeek.offset(0, -mPeekSize.top);
        mMaxScrollY = bounds.height();
        break;
    case Gravity.BOTTOM:
        if (DEBUG)
            Log.d(TAG, "gravity: bottom");
        bounds.left = l;
        bounds.top = b - v.getMeasuredHeight() - mPeekSize.bottom;
        bounds.right = l + v.getMeasuredWidth();
        bounds.bottom = b - mPeekSize.bottom;
        v.layout(bounds.left, bounds.top, bounds.right, bounds.bottom);
        boundsWithoutPeek = new Rect(bounds);
        boundsWithoutPeek.offset(0, mPeekSize.bottom);
        mMinScrollY = -bounds.height();
        break;
    case Gravity.LEFT:
        if (DEBUG)
            Log.d(TAG, "gravity: left");
        bounds.left = l + mPeekSize.left;
        bounds.top = t;
        bounds.right = l + v.getMeasuredWidth() + mPeekSize.left;
        bounds.bottom = t + v.getMeasuredHeight();
        v.layout(bounds.left, bounds.top, bounds.right, bounds.bottom);
        mMaxScrollX = bounds.width();
        boundsWithoutPeek = new Rect(bounds);
        boundsWithoutPeek.offset(-mPeekSize.left, 0);
        break;
    default:
        if (DEBUG)
            Log.d(TAG, "gravity: default");
        bounds.left = l;
        bounds.top = t;
        bounds.right = l + v.getMeasuredWidth();
        bounds.bottom = t + v.getMeasuredHeight();
        v.layout(bounds.left, bounds.top, bounds.right, bounds.bottom);
        boundsWithoutPeek = new Rect(bounds);
        break;
    }

    if (DEBUG) {
        Log.d(TAG, " == VIEW LAYOUT == " + v.toString());
        Log.d(TAG, "bounds: " + bounds.left + "," + bounds.top + "," + bounds.right + "," + bounds.bottom);
    }

    if (mLayoutBounds.containsKey(v))
        mLayoutBounds.remove(v);
    mLayoutBounds.put(v, bounds);
    if (mLayoutBoundsWithoutPeek.containsKey(v))
        mLayoutBoundsWithoutPeek.remove(v);
    mLayoutBoundsWithoutPeek.put(v, boundsWithoutPeek);
}

From source file:com.appunite.list.GridView.java

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    int closestChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        final int scrollX = getScrollX();
        final int scrollY = getScrollY();
        previouslyFocusedRect.offset(scrollX, scrollY);

        // figure out which item should be selected based on previously
        // focused rect
        Rect otherRect = mTempRect;/*from www  .ja  va  2 s.  com*/
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            // only consider view's on appropriate edge of grid
            if (!isCandidateSelection(i, direction)) {
                continue;
            }

            final View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closestChildIndex = i;
            }
        }
    }

    if (closestChildIndex >= 0) {
        setSelection(closestChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

@Override
public boolean requestChildRectangleOnScreen(final View child, final Rect rect, final boolean immediate) {

    final int rectTopWithinChild = rect.top;

    // offset so rect is in coordinates of the this view
    rect.offset(child.getLeft(), child.getTop());
    rect.offset(-child.getScrollX(), -child.getScrollY());

    final int height = getHeight();
    int listUnfadedTop = getScrollY();
    int listUnfadedBottom = listUnfadedTop + height;
    final int fadingEdge = getVerticalFadingEdgeLength();

    if (showingTopFadingEdge()) {
        // leave room for top fading edge as long as rect isn't at very top
        if (rectTopWithinChild > fadingEdge) {
            listUnfadedTop += fadingEdge;
        }//from  w w  w  . ja va  2  s .c om
    }

    final int childCount = getChildCount();
    final int bottomOfBottomChild = getChildAt(childCount - 1).getBottom();

    if (showingBottomFadingEdge()) {
        // leave room for bottom fading edge as long as rect isn't at very
        // bottom
        if (rect.bottom < bottomOfBottomChild - fadingEdge) {
            listUnfadedBottom -= fadingEdge;
        }
    }

    int scrollYDelta = 0;

    if (rect.bottom > listUnfadedBottom && rect.top > listUnfadedTop) {
        // need to MOVE DOWN to get it in view: move down just enough so
        // that the entire rectangle is in view (or at least the first
        // screen size chunk).

        if (rect.height() > height) {
            // just enough to get screen size chunk on
            scrollYDelta += rect.top - listUnfadedTop;
        } else {
            // get entire rect at bottom of screen
            scrollYDelta += rect.bottom - listUnfadedBottom;
        }

        // make sure we aren't scrolling beyond the end of our children
        final int distanceToBottom = bottomOfBottomChild - listUnfadedBottom;
        scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
    } else if (rect.top < listUnfadedTop && rect.bottom < listUnfadedBottom) {
        // need to MOVE UP to get it in view: move up just enough so that
        // entire rectangle is in view (or at least the first screen
        // size chunk of it).

        if (rect.height() > height) {
            // screen size chunk
            scrollYDelta -= listUnfadedBottom - rect.bottom;
        } else {
            // entire rect at top
            scrollYDelta -= listUnfadedTop - rect.top;
        }

        // make sure we aren't scrolling any further than the top our
        // children
        final int top = getChildAt(0).getTop();
        final int deltaToTop = top - listUnfadedTop;
        scrollYDelta = Math.max(scrollYDelta, deltaToTop);
    }

    final boolean scroll = scrollYDelta != 0;
    if (scroll) {
        scrollListItemsBy(-scrollYDelta);
        positionSelector(child);
        mSelectedTop = child.getTop();
        invalidate();
    }
    return scroll;
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

@Override
public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {

    int rectTopWithinChild = rect.top;

    // offset so rect is in coordinates of the this view
    rect.offset(child.getLeft(), child.getTop());
    rect.offset(-child.getScrollX(), -child.getScrollY());

    final int height = getHeight();
    int listUnfadedTop = getScrollY();
    int listUnfadedBottom = listUnfadedTop + height;
    final int fadingEdge = getVerticalFadingEdgeLength();

    if (showingTopFadingEdge()) {
        // leave room for top fading edge as long as rect isn't at very top
        if (rectTopWithinChild > fadingEdge) {
            listUnfadedTop += fadingEdge;
        }/*from   w  ww. ja  va 2 s .  c  o  m*/
    }

    int childCount = getChildCount();
    int bottomOfBottomChild = getChildAt(childCount - 1).getBottom();

    if (showingBottomFadingEdge()) {
        // leave room for bottom fading edge as long as rect isn't at very
        // bottom
        if (rect.bottom < (bottomOfBottomChild - fadingEdge)) {
            listUnfadedBottom -= fadingEdge;
        }
    }

    int scrollYDelta = 0;

    if (rect.bottom > listUnfadedBottom && rect.top > listUnfadedTop) {
        // need to MOVE DOWN to get it in view: move down just enough so
        // that the entire rectangle is in view (or at least the first
        // screen size chunk).

        if (rect.height() > height) {
            // just enough to get screen size chunk on
            scrollYDelta += (rect.top - listUnfadedTop);
        } else {
            // get entire rect at bottom of screen
            scrollYDelta += (rect.bottom - listUnfadedBottom);
        }

        // make sure we aren't scrolling beyond the end of our children
        int distanceToBottom = bottomOfBottomChild - listUnfadedBottom;
        scrollYDelta = Math.min(scrollYDelta, distanceToBottom);
    } else if (rect.top < listUnfadedTop && rect.bottom < listUnfadedBottom) {
        // need to MOVE UP to get it in view: move up just enough so that
        // entire rectangle is in view (or at least the first screen
        // size chunk of it).

        if (rect.height() > height) {
            // screen size chunk
            scrollYDelta -= (listUnfadedBottom - rect.bottom);
        } else {
            // entire rect at top
            scrollYDelta -= (listUnfadedTop - rect.top);
        }

        // make sure we aren't scrolling any further than the top our
        // children
        int top = getChildAt(0).getTop();
        int deltaToTop = top - listUnfadedTop;
        scrollYDelta = Math.max(scrollYDelta, deltaToTop);
    }

    final boolean scroll = scrollYDelta != 0;
    if (scroll) {
        scrollListItemsBy(-scrollYDelta);
        positionSelector(child);
        mSelectedTop = child.getTop();
        invalidate();
    }
    return scroll;
}

From source file:com.huewu.pla.lib.internal.PLAListView.java

@Override
protected void onFocusChanged(final boolean gainFocus, final int direction, final Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    if (DEBUG) {//from w  ww.  j a v a  2s .  c  o  m
        Log.v("PLA_ListView", "onFocusChanged");
    }

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();
        }

        // figure out which item should be selected based on previously
        // focused rect
        final Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            final View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            final int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}

From source file:com.huewu.pla.lib.internal.PLA_ListView.java

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

    if (DEBUG)/*from w  w  w . java  2  s  .c  o m*/
        Log.v("PLA_ListView", "onFocusChanged");

    int closetChildIndex = -1;
    if (gainFocus && previouslyFocusedRect != null) {
        // previouslyFocusedRect.offset(mScrollX, mScrollY);
        previouslyFocusedRect.offset(getScrollX(), getScrollY());

        final ListAdapter adapter = mAdapter;
        // Don't cache the result of getChildCount or mFirstPosition here,
        // it could change in layoutChildren.
        if (adapter.getCount() < getChildCount() + mFirstPosition) {
            mLayoutMode = LAYOUT_NORMAL;
            layoutChildren();
        }

        // figure out which item should be selected based on previously
        // focused rect
        Rect otherRect = mTempRect;
        int minDistance = Integer.MAX_VALUE;
        final int childCount = getChildCount();
        final int firstPosition = mFirstPosition;

        for (int i = 0; i < childCount; i++) {
            // only consider selectable views
            if (!adapter.isEnabled(firstPosition + i)) {
                continue;
            }

            View other = getChildAt(i);
            other.getDrawingRect(otherRect);
            offsetDescendantRectToMyCoords(other, otherRect);
            int distance = getDistance(previouslyFocusedRect, otherRect, direction);

            if (distance < minDistance) {
                minDistance = distance;
                closetChildIndex = i;
            }
        }
    }

    if (closetChildIndex >= 0) {
        setSelection(closetChildIndex + mFirstPosition);
    } else {
        requestLayout();
    }
}