I have a RoundRectShape
that is drawn in a View
's onDraw()
function.
The corners of this shape are modified by any drag actions on this view through an overridden onTouchEvent
function (which calls invalidate()
to force the call to onDraw()
).
When initialising variables in onDraw
, Eclipse shows a warning saying Avoid object allocations during draw/layout operations (preallocate and reuse instead).
This issue is explained as follows:
You should avoid allocating objects during a drawing or layout operation. These are called frequently, so a smooth UI can be interrupted by garbage collection pauses caused by the object allocations.
The way this is generally handled is to allocate objects up front and to reuse them for each drawing operation
To do this I obviously have to be able to modify the corner radii as set in the constructor. The RoundedRectShape class provides no setter (and I am pretty sure the related member variables are inaccessible).
I know it is a small object and a minor issue, but how can I change these corners without reinitializing the Object when the only method the class provides for setting the radii of the rounded corners is through the constructor?
Or is this just a case where doing the best thing will have to be replaced with doing what is possible?
Thanks,
P.S. Calling the constructor in the onTouchEvent()
method will remove the warning but accomplishes absolutely nothing due to the fact that onDraw()
is called every time through invalidate()
anyway