FileDocCategorySizeDatePackage
Rect.javaAPI DocAndroid 1.5 API18727Wed May 06 22:42:00 BST 2009android.graphics

Rect

public final class Rect extends Object implements android.os.Parcelable
Rect holds four integer coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom). These fields can be accessed directly. Use width() and height() to retrieve the rectangle's width and height. Note: most methods do not check to see that the coordinates are sorted correctly (i.e. left <= right and top <= bottom).

Fields Summary
public int
left
public int
top
public int
right
public int
bottom
public static final Parcelable.Creator
CREATOR
Constructors Summary
public Rect()
Create a new empty Rect. All coordinates are initialized to 0.

public Rect(int left, int top, int right, int bottom)
Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom.

param
left The X coordinate of the left side of the rectagle
param
top The Y coordinate of the top of the rectangle
param
right The X coordinate of the right side of the rectagle
param
bottom The Y coordinate of the bottom of the rectangle

        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    
public Rect(Rect r)
Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).

param
r The rectangle whose coordinates are copied into the new rectangle.

        left = r.left;
        top = r.top;
        right = r.right;
        bottom = r.bottom;
    
Methods Summary
public final intcenterX()

return
the horizontal center of the rectangle. If the computed value is fractional, this method returns the largest integer that is less than the computed value.

        return (left + right) >> 1;
    
public final intcenterY()

return
the vertical center of the rectangle. If the computed value is fractional, this method returns the largest integer that is less than the computed value.

        return (top + bottom) >> 1;
    
public booleancontains(int x, int y)
Returns true if (x,y) is inside the rectangle. The left and top are considered to be inside, while the right and bottom are not. This means that for a x,y to be contained: left <= x < right and top <= y < bottom. An empty rectangle never contains any point.

param
x The X coordinate of the point being tested for containment
param
y The Y coordinate of the point being tested for containment
return
true iff (x,y) are contained by the rectangle, where containment means left <= x < right and top <= y < bottom

        return left < right && top < bottom  // check for empty first
               && x >= left && x < right && y >= top && y < bottom;
    
public booleancontains(int left, int top, int right, int bottom)
Returns true iff the 4 specified sides of a rectangle are inside or equal to this rectangle. i.e. is this rectangle a superset of the specified rectangle. An empty rectangle never contains another rectangle.

param
left The left side of the rectangle being tested for containment
param
top The top of the rectangle being tested for containment
param
right The right side of the rectangle being tested for containment
param
bottom The bottom of the rectangle being tested for containment
return
true iff the the 4 specified sides of a rectangle are inside or equal to this rectangle

               // check for empty first
        return this.left < this.right && this.top < this.bottom
               // now check for containment
                && this.left <= left && this.top <= top
                && this.right >= right && this.bottom >= bottom;
    
public booleancontains(android.graphics.Rect r)
Returns true iff the specified rectangle r is inside or equal to this rectangle. An empty rectangle never contains another rectangle.

param
r The rectangle being tested for containment.
return
true iff the specified rectangle r is inside or equal to this rectangle

               // check for empty first
        return this.left < this.right && this.top < this.bottom
               // now check for containment
               && left <= r.left && top <= r.top
               && right >= r.right && bottom >= r.bottom;
    
public intdescribeContents()
Parcelable interface methods

        return 0;
    
public booleanequals(java.lang.Object obj)

        Rect r = (Rect) obj;
        if (r != null) {
            return left == r.left && top == r.top && right == r.right
                    && bottom == r.bottom;
        }
        return false;
    
public final floatexactCenterX()

return
the exact horizontal center of the rectangle as a float.

        return (left + right) * 0.5f;
    
public final floatexactCenterY()

return
the exact vertical center of the rectangle as a float.

        return (top + bottom) * 0.5f;
    
public final intheight()

return
the rectangle's height. This does not check for a valid rectangle (i.e. top <= bottom) so the result may be negative.

        return bottom - top;
    
public voidinset(int dx, int dy)
Inset the rectangle by (dx,dy). If dx is positive, then the sides are moved inwards, making the rectangle narrower. If dx is negative, then the sides are moved outwards, making the rectangle wider. The same holds true for dy and the top and bottom.

param
dx The amount to add(subtract) from the rectangle's left(right)
param
dy The amount to add(subtract) from the rectangle's top(bottom)

        left += dx;
        top += dy;
        right -= dx;
        bottom -= dy;
    
public booleanintersect(int left, int top, int right, int bottom)
If the rectangle specified by left,top,right,bottom intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. Note: To just test for intersection, use intersects()

param
left The left side of the rectangle being intersected with this rectangle
param
top The top of the rectangle being intersected with this rectangle
param
right The right side of the rectangle being intersected with this rectangle.
param
bottom The bottom of the rectangle being intersected with this rectangle.
return
true if the specified rectangle and this rectangle intersect (and this rectangle is then set to that intersection) else return false and do not change this rectangle.

        if (this.left < right && left < this.right
                && this.top < bottom && top < this.bottom) {
            if (this.left < left) {
                this.left = left;
            }
            if (this.top < top) {
                this.top = top;
            }
            if (this.right > right) {
                this.right = right;
            }
            if (this.bottom > bottom) {
                this.bottom = bottom;
            }
            return true;
        }
        return false;
    
public booleanintersect(android.graphics.Rect r)
If the specified rectangle intersects this rectangle, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()

param
r The rectangle being intersected with this rectangle.
return
true if the specified rectangle and this rectangle intersect (and this rectangle is then set to that intersection) else return false and do not change this rectangle.

        return intersect(r.left, r.top, r.right, r.bottom);
    
public booleanintersects(int left, int top, int right, int bottom)
Returns true if this rectangle intersects the specified rectangle. In no event is this rectangle modified. No check is performed to see if either rectangle is empty. To record the intersection, use intersect() or setIntersect().

param
left The left side of the rectangle being tested for intersection
param
top The top of the rectangle being tested for intersection
param
right The right side of the rectangle being tested for intersection
param
bottom The bottom of the rectangle being tested for intersection
return
true iff the specified rectangle intersects this rectangle. In no event is this rectangle modified.

        return this.left < right && left < this.right
               && this.top < bottom && top < this.bottom;
    
public static booleanintersects(android.graphics.Rect a, android.graphics.Rect b)
Returns true iff the two specified rectangles intersect. In no event are either of the rectangles modified. To record the intersection, use intersect() or setIntersect().

param
a The first rectangle being tested for intersection
param
b The second rectangle being tested for intersection
return
true iff the two specified rectangles intersect. In no event are either of the rectangles modified.

        return a.left < b.right && b.left < a.right
               && a.top < b.bottom && b.top < a.bottom;
    
public final booleanisEmpty()
Returns true if the rectangle is empty (left >= right or top >= bottom)

        return left >= right || top >= bottom;
    
public voidoffset(int dx, int dy)
Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.

param
dx The amount to add to the rectangle's left and right coordinates
param
dy The amount to add to the rectangle's top and bottom coordinates

        left += dx;
        top += dy;
        right += dx;
        bottom += dy;
    
public voidoffsetTo(int newLeft, int newTop)
Offset the rectangle to a specific (left, top) position, keeping its width and height the same.

param
newLeft The new "left" coordinate for the rectangle
param
newTop The new "top" coordinate for the rectangle

        right += newLeft - left;
        bottom += newTop - top;
        left = newLeft;
        top = newTop;
    
public voidreadFromParcel(android.os.Parcel in)
Set the rectangle's coordinates from the data stored in the specified parcel. To write a rectangle to a parcel, call writeToParcel().

param
in The parcel to read the rectangle's coordinates from


                                        
        
        left = in.readInt();
        top = in.readInt();
        right = in.readInt();
        bottom = in.readInt();
    
public voidset(int left, int top, int right, int bottom)
Set the rectangle's coordinates to the specified values. Note: no range checking is performed, so it is up to the caller to ensure that left <= right and top <= bottom.

param
left The X coordinate of the left side of the rectagle
param
top The Y coordinate of the top of the rectangle
param
right The X coordinate of the right side of the rectagle
param
bottom The Y coordinate of the bottom of the rectangle

        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    
public voidset(android.graphics.Rect src)
Copy the coordinates from src into this rectangle.

param
src The rectangle whose coordinates are copied into this rectangle.

        this.left = src.left;
        this.top = src.top;
        this.right = src.right;
        this.bottom = src.bottom;
    
public voidsetEmpty()
Set the rectangle to (0,0,0,0)

        left = right = top = bottom = 0;
    
public booleansetIntersect(android.graphics.Rect a, android.graphics.Rect b)
If rectangles a and b intersect, return true and set this rectangle to that intersection, otherwise return false and do not change this rectangle. No check is performed to see if either rectangle is empty. To just test for intersection, use intersects()

param
a The first rectangle being intersected with
param
b The second rectangle being intersected with
return
true iff the two specified rectangles intersect. If they do, set this rectangle to that intersection. If they do not, return false and do not change this rectangle.

        if (a.left < b.right && b.left < a.right
                && a.top < b.bottom && b.top < a.bottom) {
            left = Math.max(a.left, b.left);
            top = Math.max(a.top, b.top);
            right = Math.min(a.right, b.right);
            bottom = Math.min(a.bottom, b.bottom);
            return true;
        }
        return false;
    
public voidsort()
Swap top/bottom or left/right if there are flipped (i.e. left > right and/or top > bottom). This can be called if the edges are computed separately, and may have crossed over each other. If the edges are already correct (i.e. left <= right and top <= bottom) then nothing is done.

        if (left > right) {
            int temp = left;
            left = right;
            right = temp;
        }
        if (top > bottom) {
            int temp = top;
            top = bottom;
            bottom = temp;
        }
    
public java.lang.StringtoShortString()
Return a string representation of the rectangle in a compact form.

        return "[" + left + "," + top + "][" + right + "," + bottom + "]";
    
public java.lang.StringtoString()

        return "Rect(" + left + ", " + top + " - " + right + ", " + bottom + ")";
    
public voidunion(int left, int top, int right, int bottom)
Update this Rect to enclose itself and the specified rectangle. If the specified rectangle is empty, nothing is done. If this rectangle is empty it is set to the specified rectangle.

param
left The left edge being unioned with this rectangle
param
top The top edge being unioned with this rectangle
param
right The right edge being unioned with this rectangle
param
bottom The bottom edge being unioned with this rectangle

        if ((left < right) && (top < bottom)) {
            if ((this.left < this.right) && (this.top < this.bottom)) {
                if (this.left > left)
                    this.left = left;
                if (this.top > top)
                    this.top = top;
                if (this.right < right)
                    this.right = right;
                if (this.bottom < bottom)
                    this.bottom = bottom;
            } else {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }
    
public voidunion(android.graphics.Rect r)
Update this Rect to enclose itself and the specified rectangle. If the specified rectangle is empty, nothing is done. If this rectangle is empty it is set to the specified rectangle.

param
r The rectangle being unioned with this rectangle

        union(r.left, r.top, r.right, r.bottom);
    
public voidunion(int x, int y)
Update this Rect to enclose itself and the [x,y] coordinate. There is no check to see that this rectangle is non-empty.

param
x The x coordinate of the point to add to the rectangle
param
y The y coordinate of the point to add to the rectangle

        if (x < left) {
            left = x;
        } else if (x > right) {
            right = x;
        }
        if (y < top) {
            top = y;
        } else if (y > bottom) {
            bottom = y;
        }
    
public final intwidth()

return
the rectangle's width. This does not check for a valid rectangle (i.e. left <= right) so the result may be negative.

        return right - left;
    
public voidwriteToParcel(android.os.Parcel out, int flags)
Write this rectangle to the specified parcel. To restore a rectangle from a parcel, use readFromParcel()

param
out The parcel to write the rectangle's coordinates into

        out.writeInt(left);
        out.writeInt(top);
        out.writeInt(right);
        out.writeInt(bottom);