Does Canvas.getClipBounds allocate a Rect object?

Go To StackoverFlow.com

4

In my custom view I'm looking into using Canvas.getClipBounds() to optimize my onDraw method (so that I'm only drawing what's absolutely necessary each time it's called).

However, I still want to absolutely avoid any object creation...

My question, therefore is: does getClipBounds() allocate a new Rect each time it is called? Or is it simply recycling a single Rect?

And if it is allocating a new object, can I save this expense by using getClipBounds(Rect bounds), which seems to use the passed Rect instead of its own?


(before you scream premature optimization, do consider that when placed in a ScrollView, onDraw can be called many times each second)

2012-04-05 22:05
by yydl
Seems to me that, if you're performance-sensitive, you'd probably prefer the simpler-semantics version of getClipBounds() that takes a caller-supplied rect anyway, regardless of whether the other version does or does not happen to have some freaky recycling optimization - Don Hatch 2018-04-28 19:21
@DonHatch True. But that was indeed part of the question - if the alternate version does or does not allocate a Rect. The accepted answer covers both of these questions. Do keep in mind that back when this question was written (~6 years ago) peeking at Android source code was far from trivial.. - yydl 2018-04-29 03:08


9

I have looked into the source code for Canvas as of Android 4.0.1, and it is as follows:

/**
 * 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;
}

So, answering your question, getClipBounds(Rect bounds) will spare you from one object creation, but getClipBounds() will actually create a new Rect() object every time it is called.

2012-04-05 22:27
by Luis Miguel Serrano


0

the actual method declaration for "getClipBounds()" is this

  1. public boolean getClipBounds (Rect bounds) so it will not create a new Rect each time you will call. It will use the Rect object which you will pass as argument to this function. i suggest have a look at this enter link description here
2012-04-05 22:16
by appdroid
What? I don't understand. As I wrote in my question, there are two different method calls. One does take a Rect and one doesn't. I'm asking about the one that doesn't - yydl 2012-04-05 22:26
Sorry for the inconvenience , but to avoid creating Rect object each time the method is called you should use this method getClipBounds(Rect bounds) as Luis said - appdroid 2012-04-05 22:36
Ads