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  = (int) (mDrawRect.width() - getPaddingLeft()) / 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;
            } 
//          Log.v(TAG, "radius: " + radius + "; touchRadius: " + distanceFromCenter + "; distX: " + distX + "; distY: " + distY + "; retval: " + retval);  
        } else { 
            boolean verticalCheck = (y >= r.top - hysteresis) && (y < r.bottom + hysteresis);
            boolean horizCheck = (x >= r.left - hysteresis) && (x < r.right + hysteresis);
 
            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;
 
            if (retval == GROW_NONE && r.contains((int)x, (int)y))
                retval = MOVE;
        } 
        return retval;
    } 
 
    void handleMotion(int edge, float dx, float dy) {
        Rect r = computeLayout();
        if (edge == GROW_NONE) {
            return; 
        } else if (edge == MOVE) {
            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;
 
            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);
                    
        } 
//        Log.v(TAG, "ratio is now " + this.mCropRect.width() / this.mCropRect.height()); 
    } 
     
    void moveBy(float dx, float dy) {
        Rect invalRect = new Rect(mDrawRect);
 
        mCropRect.offset(dx, dy);
        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);
    } 
     
    private void shift(RectF r, float dx, float dy) {
        r.left   += dx;
        r.right  += dx;
        r.top    += dy;
        r.bottom += dy;
    } 
 
    void growBy(float dx, float dy) {
//      Log.v(TAG, "growBy: " + dx + " " + dy + "; rect w/h is " + mCropRect.width() + " / " + mCropRect.height()); 
        if (mMaintainAspectRatio) {
            if (dx != 0) {
                dy = dx / mInitialAspectRatio;
            } else if (dy != 0) {
                dx = dy * mInitialAspectRatio;
            } 
        } 
 
        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);
         
        float widthCap = 25F;
        if (r.width() < 25) {
            r.inset(-(25F-r.width())/2F, 0F);
        } 
        float heightCap = mMaintainAspectRatio ? (widthCap / mInitialAspectRatio) : widthCap;
        if (r.height() < heightCap) {
            r.inset(0F, -(heightCap-r.height())/2F);
        } 
         
        if (r.left < mImageRect.left) {
            shift(r, mImageRect.left - r.left, 0F);
        } else if (r.right > mImageRect.right) {
            shift(r, -(r.right - mImageRect.right), 0);
        } 
        if (r.top < mImageRect.top) {
            shift(r, 0F, mImageRect.top - r.top);
        } else if (r.bottom > mImageRect.bottom) {
            shift(r, 0F, -(r.bottom - mImageRect.bottom));
        } 
/*         
        RectF rCandidate = new RectF(r); 
        r.intersect(mImageRect); 
        if (mMaintainAspectRatio) { 
            if (r.left != rCandidate.left) { 
                Log.v(TAG, "bail 1"); 
                return; 
            } 
            if (r.right != rCandidate.right) { 
                Log.v(TAG, "bail 2"); 
                return; 
            } 
            if (r.top != rCandidate.top) { 
                Log.v(TAG, "bail 3"); 
                return; 
            } 
            if (r.bottom != rCandidate.bottom) { 
                Log.v(TAG, "bail 4"); 
                return; 
            } 
        } 
*/         
        mCropRect.set(r);
        mDrawRect = computeLayout();
        mContext.invalidate();
    } 
     
    public Rect getCropRect() {
        return new Rect((int)mCropRect.left, (int)mCropRect.top, (int)mCropRect.right, (int)mCropRect.bottom);
    } 
     
    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();
    } 
Stop searching for code, let great code find you!  Add Codota to your java IDE