Code example for Rect

Methods: centerXcenterYcontains

0
        Rect r = computeLayout();
        final float hysteresis = 20F;
        int retval = GROW_NONE;
 
        if (mCircle) {
            float distX = x - r.centerX();
            float distY = y - r.centerY();
            int distanceFromCenter =
                    (int) Math.sqrt(distX * distX + distY * distY);
            int radius  = mDrawRect.width() / 2;
            int delta = distanceFromCenter - radius;
            if (Math.abs(delta) <= hysteresis) {
                if (Math.abs(distY) > Math.abs(distX)) {
                    if (distY < 0) {
                        retval = GROW_TOP_EDGE;
                    } else { 
                        retval = GROW_BOTTOM_EDGE;
                    } 
                } else { 
                    if (distX < 0) {
                        retval = GROW_LEFT_EDGE;
                    } else { 
                        retval = GROW_RIGHT_EDGE;
                    } 
                } 
            } else if (distanceFromCenter < radius) {
                retval = MOVE;
            } else { 
                retval = GROW_NONE;
            } 
        } else { 
            // verticalCheck makes sure the position is between the top and 
            // the bottom edge (with some tolerance). Similar for horizCheck. 
            boolean verticalCheck = (y >= r.top - hysteresis)
                    && (y < r.bottom + hysteresis);
            boolean horizCheck = (x >= r.left - hysteresis)
                    && (x < r.right + hysteresis);
 
            // Check whether the position is near some edge(s). 
            if ((Math.abs(r.left - x)     < hysteresis)  &&  verticalCheck) {
                retval |= GROW_LEFT_EDGE;
            } 
            if ((Math.abs(r.right - x)    < hysteresis)  &&  verticalCheck) {
                retval |= GROW_RIGHT_EDGE;
            } 
            if ((Math.abs(r.top - y)      < hysteresis)  &&  horizCheck) {
                retval |= GROW_TOP_EDGE;
            } 
            if ((Math.abs(r.bottom - y)   < hysteresis)  &&  horizCheck) {
                retval |= GROW_BOTTOM_EDGE;
            } 
 
            // Not near any edge but inside the rectangle: move. 
            if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
                retval = MOVE;
            } 
        } 
        return retval;
    } 
 
    // Handles motion (dx, dy) in screen space. 
    // The "edge" parameter specifies which edges the user is dragging. 
    void handleMotion(int edge, float dx, float dy) {
        Rect r = computeLayout();
        if (edge == GROW_NONE) {
            return; 
        } else if (edge == MOVE) {
            // Convert to image space before sending to moveBy(). 
            moveBy(dx * (mCropRect.width() / r.width()),
                   dy * (mCropRect.height() / r.height()));
        } else { 
            if (((GROW_LEFT_EDGE | GROW_RIGHT_EDGE) & edge) == 0) {
                dx = 0;
            } 
 
            if (((GROW_TOP_EDGE | GROW_BOTTOM_EDGE) & edge) == 0) {
                dy = 0;
            } 
 
            // Convert to image space before sending to growBy(). 
            float xDelta = dx * (mCropRect.width() / r.width());
            float yDelta = dy * (mCropRect.height() / r.height());
            growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta,
                    (((edge & GROW_TOP_EDGE) != 0) ? -1 : 1) * yDelta);
        } 
    } 
 
    // Grows the cropping rectange by (dx, dy) in image space. 
    void moveBy(float dx, float dy) {
        Rect invalRect = new Rect(mDrawRect);
 
        mCropRect.offset(dx, dy);
 
        // Put the cropping rectangle inside image rectangle. 
        mCropRect.offset(
                Math.max(0, mImageRect.left - mCropRect.left),
                Math.max(0, mImageRect.top  - mCropRect.top));
 
        mCropRect.offset(
                Math.min(0, mImageRect.right  - mCropRect.right),
                Math.min(0, mImageRect.bottom - mCropRect.bottom));
 
        mDrawRect = computeLayout();
        invalRect.union(mDrawRect);
        invalRect.inset(-10, -10);
        mContext.invalidate(invalRect);
    } 
 
    // Grows the cropping rectange by (dx, dy) in image space. 
    void growBy(float dx, float dy) {
        if (mMaintainAspectRatio) {
            if (dx != 0) {
                dy = dx / mInitialAspectRatio;
            } else if (dy != 0) {
                dx = dy * mInitialAspectRatio;
            } 
        } 
 
        // Don't let the cropping rectangle grow too fast. 
        // Grow at most half of the difference between the image rectangle and 
        // the cropping rectangle. 
        RectF r = new RectF(mCropRect);
        if (dx > 0F && r.width() + 2 * dx > mImageRect.width()) {
            float adjustment = (mImageRect.width() - r.width()) / 2F;
            dx = adjustment;
            if (mMaintainAspectRatio) {
                dy = dx / mInitialAspectRatio;
            } 
        } 
        if (dy > 0F && r.height() + 2 * dy > mImageRect.height()) {
            float adjustment = (mImageRect.height() - r.height()) / 2F;
            dy = adjustment;
            if (mMaintainAspectRatio) {
                dx = dy * mInitialAspectRatio;
            } 
        } 
 
        r.inset(-dx, -dy);
 
        // Don't let the cropping rectangle shrink too fast. 
        final float widthCap = 25F;
        if (r.width() < widthCap) {
            r.inset(-(widthCap - r.width()) / 2F, 0F);
        } 
        float heightCap = mMaintainAspectRatio
                ? (widthCap / mInitialAspectRatio)
                : widthCap;
        if (r.height() < heightCap) {
            r.inset(0F, -(heightCap - r.height()) / 2F);
        } 
 
        // Put the cropping rectangle inside the image rectangle. 
        if (r.left < mImageRect.left) {
            r.offset(mImageRect.left - r.left, 0F);
        } else if (r.right > mImageRect.right) {
            r.offset(-(r.right - mImageRect.right), 0);
        } 
        if (r.top < mImageRect.top) {
            r.offset(0F, mImageRect.top - r.top);
        } else if (r.bottom > mImageRect.bottom) {
            r.offset(0F, -(r.bottom - mImageRect.bottom));
        } 
 
        mCropRect.set(r);
        mDrawRect = computeLayout();
        mContext.invalidate();
    } 
 
    // Returns the cropping rectangle in image space. 
    public Rect getCropRect() {
        return new Rect((int) mCropRect.left, (int) mCropRect.top,
                        (int) mCropRect.right, (int) mCropRect.bottom);
    } 
 
    // Maps the cropping rectangle from image space to screen space. 
    private Rect computeLayout() {
        RectF r = new RectF(mCropRect.left, mCropRect.top,
                            mCropRect.right, mCropRect.bottom);
        mMatrix.mapRect(r);
        return new Rect(Math.round(r.left), Math.round(r.top),
                        Math.round(r.right), Math.round(r.bottom));
    } 
 
    public void invalidate() { 
        mDrawRect = computeLayout();