开发者

Android Different paints for Textsize

Is it more efficeint to create a new Paint() or modify and exsisting one when you want to print something in a different font si开发者_如何学Pythonze or style? - this is for a game so the changes would be made each frame


As far as I know object creation is always expensive. You should avoid that if you can, especially in your case when this happens each frame. Android always suggest you to reuse objects (see this doc). I don't think that Paint should have such a big memory footprint but why not just reuse. Or create two of them and use them?

When should I reuse an object (paint in this case)?
If you're just changing text size and color I would reuse one paint. If you're changing changing nearly every attribute of paint to draw another text I would suggest to create another paint object for that because you culd initialize it once, reuse it for the same kind of text and your code will be cleaner.


If the Paint object does not have to change each frame, then I would trade a small memory penalty for better performance by creating one Paint object for each size you need, but not on each frame. Do it in the class constructor and use it each frame, so they are created once only.
Moreover, if the Paint will not change for different instances, make the Paints static and you'll have one set for all instances.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜