android Rect类的使用
今天,講講Android的Rect類的使用。
public final class
Rect
extends?Object
implements?Parcelable
| java.lang.Object | |
| ???? | android.graphics.Rect |
| Rect()Create a new empty Rect. | |
| Rect(int left, int top, int right, int bottom)Create a new rectangle with the specified coordinates. | |
| Rect(Rect?r)Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified). |
| final int | centerX()獲取矩陣中心點(x,y) |
| final int | centerY() |
| boolean | contains(int x, int y)是否包含(x,y)點Returns true if (x,y) is inside the rectangle. |
| boolean | contains(int left, int top, int right, int bottom)是否包含(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. |
| boolean | contains(Rect?r)Returns true iff the specified rectangle r is inside or equal to this rectangle. |
| int | describeContents()Parcelable interface methods |
| boolean | equals(Object?obj)Compares this instance with the specified object and indicates if they are equal. |
| final float | exactCenterX()該方法和CenterX()類似,只是該方法精確度比它高(返回float類型) |
| final float | exactCenterY() |
| String | flattenToString()Return a string representation of the rectangle in a well-defined format. |
| final int | height() |
| void | inset(int dx, int dy)Inset the rectangle by (dx,dy). |
| boolean | intersect(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. |
| boolean | intersect(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. |
| boolean | intersects(int left, int top, int right, int bottom)Returns true if this rectangle intersects the specified rectangle. |
| static boolean | intersects(Rect?a,?Rect?b)Returns true iff the two specified rectangles intersect. |
| final boolean | isEmpty()Returns true if the rectangle is empty (left >= right or top >= bottom) |
| void | offset(int dx, int dy)該矩陣在x軸和y軸分別發生的偏移量(很有用,可以上下移動矩陣)Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates. |
| void | offsetTo(int newLeft, int newTop)保持矩陣的寬高,矩陣的左上角偏移到(newLeft,newTop)該點Offset the rectangle to a specific (left, top) position, keeping its width and height the same. |
| void | readFromParcel(Parcel?in)Set the rectangle's coordinates from the data stored in the specified parcel. |
| void | set(int left, int top, int right, int bottom)Set the rectangle's coordinates to the specified values. |
| void | set(Rect?src)Copy the coordinates from src into this rectangle. |
| void | setEmpty()Set the rectangle to (0,0,0,0) |
| boolean | setIntersect(Rect?a,?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. |
| void | sort()Swap top/bottom or left/right if there are flipped (i.e. |
| String | toShortString()Return a string representation of the rectangle in a compact form. |
| String | toString()Returns a string containing a concise, human-readable description of this object. |
| static?Rect | unflattenFromString(String?str)Returns a Rect from a string of the form returned by?flattenToString(), or null if the string is not of that form. |
| void | union(int left, int top, int right, int bottom)Update this Rect to enclose itself and the specified rectangle. |
| void | union(Rect?r)Update this Rect to enclose itself and the specified rectangle. |
| void | union(int x, int y)Update this Rect to enclose itself and the [x,y] coordinate. |
| final int | width() |
| void | writeToParcel(Parcel?out, int flags)Write this rectangle to the specified parcel. |
1.???new?Rect(150,?75,?260,?120)??
??這個構造方法需要四個參數這四個參數?指明了什么位置??我們就來解釋怎么畫?這個?矩形?
這四個?參數?分別代表的意思是:left?? top?? right?? bottom??上下左右唄。啊,不是?是?左?上?右?下。?下面給大家解釋??
left?:?矩形左邊的X坐標??150 ?
top:????矩形頂部的Y坐標???75 ? ??
right :??矩形右邊的X坐標???260 ? ?
bottom:?矩形底部的Y坐標?120 ? ?
說白了就是左上角的坐標是(150,75),右下角的坐標是(260,120),這樣就好理解了
2、Rect類與RectF類(android.graphics.RectF)的區別?? 答:主要還是在精度上的不同,他們分別是:int、float類型的
這里需要注意一點,Rect類主要用于表示坐標系中的一塊矩形區域,并可以對其做一些簡單操作。這塊矩形區域,需要用左上右下兩個坐標點表示(left,top,right,bottom),你也可以獲取一個Rect實例的Width和Height。就在這里,奇葩的事情來了,作為一個有一點經驗的做圖像或者矩陣運算或者編程的程序員來說,大家的共識是,如果一個矩陣是MxN的,也就是M行N列,那么行號是[0,M-1],列號是[0,N-1]。可是奇葩的Rect類并不是這樣的!如果你這么聲明一個Rect類:
| Rect rect=new Rect(100,50,300,500); |
那么右下角(300,500)其實是不在這個矩形里面的,但是左上角(100,50)在,也就是說,這個矩形實際表示的區域是:(100,50,299,499)。另外,Rect計算出的Height和Width倒是對的。所以,在此告誡各位程序員,在涉及Rect運算的時候,盡量不要使用它的右下角左邊,即right和bottom。因為他們是錯的。當然,在你調用android自己的函數時,是可以使用的,因為Android里面一直保持這么奇葩的思維。
android Rect類的使用就講完了。
就這么簡單。
總結
以上是生活随笔為你收集整理的android Rect类的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android MotionEvent
- 下一篇: android NDk编程