Reputation: 4644
In my android app I am drawing lots of Rectangles, Lines & Arcs which frequently change on user action.
Now what I have done is created my Paint objects inside the onDraw() method so that they remain local variables and do not cause any memory issues.
But during a code review this was pointed out as a mistake. I am told that this might slow down the drawing on canvas as new() will be called each time onDraw() is called. So I am asked to make Paint objects as class variables, initialize them only once and only change their property in onDraw() and further nullify them when the screen is closed/destroyed.
I want to ask that would this be the right thing to do or things are better the way they already are because I did not find the UI slow on my screen and I doubt that this might be an unnecessary change.
Please provide suggestions.
Upvotes: 1
Views: 1183
Reputation: 2186
Yes you should use Paint as a class member. The size of Paint is probably small, not that I've checked mind, but it is merely a settings container.
Allocating any objects in Draw is a bad idea as it can cause the GC to kick in and cause a very noticeable glitch, especially on older devices.
Upvotes: 1
Reputation: 2541
I have found that there is a lag for Paint paint = new Paint();
during onDraw()
processing. I now make paint
a class variable, and in onDraw()
simply modify the attributes of the already-constructed Paint variable.
public void onDraw(Canvas canvas) {
mPaint.setColor(Color.BLUE);
...
mPaint.setStyle(Style.FILL);
...
}
Upvotes: 2