/*Rect*/
/**
* Rectholds 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).
*/
/* Rect 拥有矩形的四个坐标。四条边的坐标表示一个矩形,这四条边是左上右下。
* 这些域值可以直接存取。使用width()和height()获取矩形的宽高。
* 注意:大多数方法不会去检查坐标是否排序正确。left <= right 和 top <= bottom
*/
public final classRect implements Parcelable{
public int left;
public int top;
public int right;
public int bottom;
private static final Pattern FLATTENED_PATTERN = Pattern.compile(
"(-?\\d+) (-?\\d+) (-?\\d+)(-?\\d+)");
/**
* Create a new empty Rect. All coordinates are initialized to 0.
*/
/* 创建一个新的空的Rect。所有的坐标被初始化为0
*/
public Rect() {}
/**
* Create a new rectangle with the specified coordinates. Note: no range
* checking is performed, so the caller must ensure that left <= rightand
* top <= bottom.
*
* @param left The X coordinate of the leftside of the rectangle
* @param top The Y coordinate of the topof the rectangle
* @param right The X coordinate of theright side of the rectangle
* @param bottom The Y coordinate of the bottom of the rectangle
*/
/* 创建一个矩形,指定它的坐标。注意:没有执行范围检查,
* 所以调用者一定要确保left <= right 和 top <= bottom
*/
public Rect(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
/**
* 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.
*/
/* 创建一个新的矩形,初始化为传入的参数中的坐标,实际就是矩形的复制
* @param r
*/
public Rect(Rect r) {
if(r == null){
left= top = right = bottom= 0;
} else{
left= r.left;
top= r.top;
right= r.right;
bottom= r.bottom;
}
}
/*
* 判断是否是同一个实例或两个Rect对象里的四个坐标值是否一样
*/
@Override
public booleanequals(Object o) {
if(this == o) return true;
if(o == null|| getClass() != o.getClass()) return false;
Rect r = (Rect) o;
return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
}
@Override
public inthashCode() {
intresult = left;
result = 31 * result + top;
result = 31 * result + right;
result = 31 * result + bottom;
returnresult;
}
/*
* 将矩形的坐标转化为字符串,格式:Rect(left, top - right,bottom)
*/
@Override
public String toString() {
StringBuilder sb = newStringBuilder(32);
sb.append("Rect(");sb.append(left);sb.append(", ");
sb.append(top);sb.append(" - ");sb.append(right);
sb.append(", ");sb.append(bottom);sb.append(")");
returnsb.toString();
}
/**
* Return a string representation of the rectangle in a compact form.
*/
/* 返回一个紧凑型格式的代表矩形的字符串,格式:[left,top][right,bottom]
* @return
*/
public String toShortString() {
returntoShortString(newStringBuilder(32));
}
/**
* Return a string representation of the rectangle in a compact form.
* @hide
*/
public String toShortString(StringBuilder sb) {
sb.setLength(0);
sb.append('[');sb.append(left);sb.append(',');
sb.append(top);sb.append("][");sb.append(right);
sb.append(',');sb.append(bottom);sb.append(']');
returnsb.toString();
}
/**
* Return a string representation of the rectangle in a well-defined format.
*
* <p>You can later recover the Rect from this string through
* {@link #unflattenFromString(String)}.
*
* @return Returns a new String of the form "left top right bottom"
*/
/* 返回一个定义好格式的代表矩形的字符串
* 然后你可以通过unflattenFromString(String)方法从这个字符串恢复Rect
* 字符串格式"left top right bottom"
* 方法的名字的意思差不多是 平整到字符串,因为格式看起来很平整,即格式化到字符串
*/
public String flattenToString() {
StringBuilder sb = newStringBuilder(32);
// WARNING: Do not change the format ofthis string, it must be
// preserved because Rects are savedin this flattened format.
sb.append(left);
sb.append(' ');
sb.append(top);
sb.append(' ');
sb.append(right);
sb.append(' ');
sb.append(bottom);
returnsb.toString();
}
/**
* Returns a Rect from a string of the form returned by {@link #flattenToString},
* or null if the string is not of that form.
*/
/* 静态方法,从一个平整的格式化字符串返回一个Rect对象,字符串格式为"left top right bottom"
* @param str
* @return
*/
public staticRect unflattenFromString(String str) {
Matcher matcher = FLATTENED_PATTERN.matcher(str);
if(!matcher.matches()) {
return null;
}
return new Rect(Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)),
Integer.parseInt(matcher.group(4)));
}
/**
* Print short representation to given writer.
* @hide
*/
/* 用于打印紧凑型字符串坐标
* @param pw
*/
public voidprintShortString(PrintWriter pw) {
pw.print('[');pw.print(left);pw.print(',');
pw.print(top);pw.print("][");pw.print(right);
pw.print(',');pw.print(bottom);pw.print(']');
}
/**
* Returns true if the rectangle is empty (left >= right or top >=bottom)
*/
/* 当(left >= right 或 top >= bottom)返回ture,认为是非矩形。
* 点或线都是非矩形,返回false
* 该方法直接点理解就是判断是否 是非矩形
*/
public final boolean isEmpty() {
return left >= right || top>= bottom;
}
/**
* @return the rectangle's width. This does not check for a valid rectangle
* (i.e. left <= right) so the result may be negative.
*/
/* 返回矩形的宽,这个方法不会检查矩形是否有效,所以结果可能为负数
* @return
*/
public final int width() {
return right - left;
}
/**
* @return the rectangle's height. This does not check for a valid rectangle
* (i.e. top <= bottom) so the result may be negative.
*/
/* 返回矩形的高,这个方法不会检查矩形是否有效,所以结果可能为负数
* @return
*/
public final int height() {
return bottom - top;
}
/**
* @return the horizontal center of the rectangle. If the computed value
* is fractional, thismethod returns the largest integer that is
* less than the computedvalue.
*/
/* 矩形的水平方向上中心点x坐标。如果计算值是分数,这个方法返回的是最大整数,小于计算值
*/
public final int centerX() {
return(left + right) >> 1;
}
/**
* @return the vertical center of the rectangle. If the computed value
* is fractional, thismethod returns the largest integer that is
* less than the computedvalue.
*/
/* 矩形的垂直方向上中心点坐标。如果计算值是分数,这个方法返回的是最大整数,小于计算值
*/
public final int centerY() {
return(top + bottom) >> 1;
}
/**
* @return the exact horizontal center of the rectangle as a float.
*/
/* 矩形的水平方向上中心点x精确坐标。返回浮点值
*/
public final float exactCenterX() {
return(left + right) * 0.5f;
}
/* 矩形的垂直方向上中心点y精确坐标。返回浮点值
*/
public final float exactCenterY() {
return(top + bottom) * 0.5f;
}
/**
* Set the rectangle to (0,0,0,0)
*/
/*
* 设置成一个点了,即设置成非矩形
*/
public voidsetEmpty() {
left= right = top = bottom= 0;
}
/**
* Set the rectangle's coordinates to the specified values. Note: norange
* 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 leftside of the rectangle
* @param top The Y coordinate of the topof the rectangle
* @param right The X coordinate of theright side of the rectangle
* @param bottom The Y coordinate of the bottom of the rectangle
*/
/* 设置矩形的坐标,当使用空的构造方法时,再使用该方法赋初值,或者改变已有矩形的四条边的坐标
*/
public voidset(intleft, inttop, intright, intbottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
/**
* Copy the coordinates from src into this rectangle.
*
* @param src The rectangle whose coordinates are copied into this
* rectangle.
*/
/* 设置矩形坐标和传入的矩形的坐标一样
*/
public voidset(Rect src) {
this.left = src.left;
this.top = src.top;
this.right = src.right;
this.bottom = src.bottom;
}
/**
* Offset the rectangle by adding dx to its left and rightcoordinates, 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
*/
/* x和y方向上移动dx和dy的距离
*/
public voidoffset(intdx, intdy) {
left+= dx;
top+= dy;
right+= dx;
bottom+= dy;
}
/**
* 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
*/
/* 移动到指定的点
*/
public voidoffsetTo(intnewLeft, intnewTop) {
right+= newLeft - left;
bottom+= newTop - top;
left= newLeft;
top= newTop;
}
/**
* Inset the rectangle by (dx,dy). If dx ispositive, then the sides are
* moved inwards, making the rectangle narrower. If dx isnegative, then the
* sides are moved outwards, making the rectangle wider. The same holdstrue
* 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)
*/
/* 根据名字不太好理解,插入一个边框,为正值时两条边界同时向内移动dx距离,为负值时,同时向外移动dx,效果是矩形的放大缩小,每个方向上缩放一致
* @param dx
* @param dy
*/
public voidinset(intdx, intdy) {
left+= dx;
top+= dy;
right-= dx;
bottom-= dy;
}
/**
* 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. Thismeans
* 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
*/
/* 判断是否包含这个点,是个半开区间的判断
*/
public booleancontains(intx, inty) {
return left < right && top < bottom // check for empty first
&& x >= left&& x < right&& y >= top&& y < bottom;
}
/**
* Returns true iff the 4 specified sides of a rectangle areinside 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 thisrectangle
*/
/* 判断四条边是否在矩形内,闭区间判断,等于也表示在矩形内
*/
public booleancontains(intleft, inttop, intright, intbottom) {
// 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;
}
/**
* Returns true iff the specified rectangle r is inside or equalto 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
*/
/* 判断传入的矩形是否在矩形内,闭区间判断,等于也表示在矩形内
*/
public booleancontains(Rect r) {
// 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;
}
/**
* 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 {@link #intersects(Rect,Rect)}.
*
* @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 rectangleis then set to that intersection) else
* return false and donot change this rectangle.
*/
/* 判断矩形是否与给定的四条边围成的区域相交,若相交则将传入的参数作为当前矩形,开区间比较,若仅仅边界相交就返回false
*/
public booleanintersect(intleft, inttop, intright, intbottom) {
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;
}
/**
* If the specified rectangle intersects this rectangle, return true andset
* this rectangle to that intersection, otherwise return false and do not
* change this rectangle. No check is performed to see if eitherrectangle
* 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 rectangleis then set to that intersection) else
* return false and donot change this rectangle.
*/
/* 判断矩形是否与给定的矩形相交,若相交则将传入的矩形作为当前矩形,开区间比较,若仅仅边界相交这返回false
*/
public booleanintersect(Rect r) {
returnintersect(r.left,r.top, r.right, r.bottom);
}
/**
* 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 tothat intersection. If they do not, return
* false and do notchange this rectangle.
*/
/* 将两个矩形的相交区域作为当前矩形大小和位置,设置成功则返回true,边界重合不算相交
*/
public booleansetIntersect(Rect a, Rect b) {
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;
}
/**
* 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, useintersect()
* 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 thisrectangle modified.
*/
/* 判断是否相交,仅仅边线重合不算相交
*/
public booleanintersects(intleft, inttop, intright, intbottom) {
return this.left< right && left < this.right && this.top< bottom && top < this.bottom;
}
/**
* Returns true iff the two specified rectangles intersect. In noevent are
* either of the rectangles modified. To record the intersection,
* use {@link #intersect(Rect)} or {@link #setIntersect(Rect, Rect)}.
*
* @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 therectangles modified.
*/
/* 静态判断两个矩形是否相交,仅仅边界重合不算相交,返回false
*/
public static boolean intersects(Rect a, Rect b) {
returna.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;
}
/**
* Update this Rect to enclose itself and the specified rectangle.If the
* specified rectangle is empty, nothing is done. If this rectangle isempty
* 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
*/
/* 取两个矩形联合后的最大边界范围,若自身非矩形,则将传入的范围负值给自身,若传入的非矩形范围,则什么都不做
*/
public voidunion(intleft, inttop, intright, intbottom) {
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;
}
}
}
/**
* Update this Rect to enclose itself and the specified rectangle.If the
* specified rectangle is empty, nothing is done. If this rectangle isempty
* it is set to the specified rectangle.
*
* @param r The rectangle being unioned with this rectangle
*/
/* 取最大边界范围
* @param r
*/
public voidunion(Rect r) {
union(r.left,r.top, r.right, r.bottom);
}
/**
* 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
*/
/* 当x y值在矩形之外时,将原矩形区域联合扩大,小于或等于 什么都不做
* @param x
* @param y
*/
public voidunion(intx, inty) {
if(x < left){
left= x;
} else if (x > right) {
right= x;
}
if(y < top){
top= y;
} else if (y > bottom) {
bottom= y;
}
}
/**
* 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 eachother.
* If the edges are already correct (i.e. left <= right and top <=bottom)
* then nothing is done.
*/
/* 排序,将四条边设置成矩形,如果四条边能围成矩形
*
*/
public voidsort() {
if(left > right) {
inttemp = left;
left= right;
right= temp;
}
if(top > bottom) {
inttemp = top;
top= bottom;
bottom= temp;
}
}
/**
* Parcelable interface methods
*/
public intdescribeContents() {
return0;
}
/**
* Write this rectangle to the specified parcel. To restore a rectanglefrom
* a parcel, use readFromParcel()
* @param out The parcel to write the rectangle's coordinates into
*/
public voidwriteToParcel(Parcel out, intflags) {
out.writeInt(left);
out.writeInt(top);
out.writeInt(right);
out.writeInt(bottom);
}
public static final Parcelable.Creator<Rect>CREATOR = new Parcelable.Creator<Rect>(){
/**
* Return a new rectangle from the data in the specified parcel.
*/
publicRect createFromParcel(Parcel in) {
Rect r = newRect();
r.readFromParcel(in);
returnr;
}
/**
* Return an array of rectangles of the specified size.
*/
publicRect[] newArray(intsize) {
return new Rect[size];
}
};
/**
* 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
*/
public voidreadFromParcel(Parcel in) {
left= in.readInt();
top= in.readInt();
right= in.readInt();
bottom= in.readInt();
}
/**
* Scales up the rect by the given scale.
* @hide
*/
/* 将自身进行缩放
* @param scale
*/
public voidscale(floatscale) {
if(scale != 1.0f) {
left= (int)(left * scale +0.5f);
top= (int)(top * scale +0.5f);
right= (int)(right * scale +0.5f);
bottom= (int)(bottom * scale +0.5f);
}
}
}