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?
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.
Paint.java
. It is backed up by a native
paint. So when you create new Paint objects, you are also creating the native objects as well. –
Sis setColor
is time consuming operation. I'm not sure if your approach is actually efficient. Here is a nice article: medium.com/rosberryapps/… –
Pelite 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 ))).
© 2022 - 2024 — McMap. All rights reserved.