从 Android 4.0.1 开始,我查看了 Canvas 的源代码,如下所示:
/**
* Retrieve the clip bounds, returning true if they are non-empty.
*
* @param bounds Return the clip bounds here. If it is null, ignore it but
* still return true if the current clip is non-empty.
* @return true if the current clip is non-empty.
*/
public boolean getClipBounds(Rect bounds) {
return native_getClipBounds(mNativeCanvas, bounds);
}
/**
* Retrieve the clip bounds.
*
* @return the clip bounds, or [0, 0, 0, 0] if the clip is empty.
*/
public final Rect getClipBounds() {
Rect r = new Rect();
getClipBounds(r);
return r;
}
因此,回答您的问题, getClipBounds(Rect bounds) 将使您免于创建一个对象,但 getClipBounds() 实际上会在每次调用它时创建一个新的 Rect() 对象。