Android Paint Object Memory Efficency

1.6k views Asked by At

Is android.graphics.Paint memory heavy object? Which one is more efficient, to pass paint object refrence to classes that need to draw on canvas and set paint properties such as color, style, etc. in those classes, or create new Paint object wherever it's needed?

2

There are 2 answers

3
Henry On BEST ANSWER

Yes, Paint is heavy, especially its creation and initialization. Does this mean you have to reuse the same Paint object for everything? Well, it depends.

If you are going to perform multiple drawText() but with different color, then you can reuse the same paint but with different color (using setColor()). But if you are going to perform two unrelated operation(drawing) in two different classes and there are major differences in the Paint configuration like Color, font size, Style, PathEffect, etc... then it's better to have separate Paint objects for them.

In short, use the same paint for performing similar drawing with less differences. And use different paint objects for performing unrelated drawing with major differences. Hope this helps.

1
Vova On

For me best way is: Create new Paint for each object with different style or width or color. And for draw text create other paints. (If you want draw to text with different color or text size create new paint to)

This way create your code more lazy for other developer, because one paint draws one object, it is good OOP style ))).