1. 程式人生 > >Android自定義View之getTextBounds()

Android自定義View之getTextBounds()

在Android自定義View的過程中一定會用到Paint,而paint屬性中有一個方法getTextBounds(String text,int start,int end,Rext bounds),它的中文解釋是:返回一個包含中文的矩形邊界,位置為(start,end)

英文解釋:Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).由呼叫者返回在邊界(分配)的最小矩形包含所有的字元

,以隱含原點(0,0)。

Parameters

text    String:String to measure and return its bounds  要測量的字串,並返回它的界限

start   int:Index of the first char in the string to measure  在這個字串裡的第一個字元的索引,它將作為測量的起點

end    int:1 past the last char in the string  measure

bounds    Rect:Returns the unioned bounds of all the text.返回包含文字在內,所形成的邊界。

下面是第二個方法:

void getTextBounds (char[] text, 
                int index, 
                int count, 
                Rect bounds)
Return in bounds (allocated by the caller) the smallest rectangle that encloses all of the characters, with an implied origin at (0,0).由呼叫者返回在邊界(分配)的最小矩形包含所有的字元,以隱含原點(0,0)。


Parameters
text    char: Array of chars to measure and return their unioned bounds一個包含字元的陣列,測量並返回他們的聯合邊界
index    int: Index of the first char in the array to measure 在這個 陣列 裡的第一個(要測量的)字元的索引(你不一定要指定為0, 雖然那確實意味著從第一個開始),它將作為測量的起點
count    int: The number of chars, beginning at index, to measure字元的數量(比如5),測量將會 開始於上面指定的索引號,結束於 此數量值之後(若為5, 則上面指定的索引號+5)
bounds    Rect: Returns the unioned bounds of all the text. Must be allocated by the caller.返回包含所有文字在內,所形成的邊界,(所以叫unioned bounds)。必須分配給呼叫者。