I’m reading up on SurfaceView and how to use it, and I’ve come across some information that states that a SurfaceView has View#willNotDraw() set to false by default, and that it’s up to you to call SurfaceView#onDraw(). I also read that RomainGuy said that this is done by default because it is more efficient. My question now is, when should you handle calling SurfaceView#onDraw() in a separate thread, and when should you just set View#willNotDraw() to true, and just call SurfaceView#invalidate(). Is there a difference between the two, and does one improve performance more than the other?
I’m reading up on SurfaceView and how to use it, and I’ve come across
Share
See:
http://developer.android.com/reference/android/view/View.html#setWillNotDraw(boolean)
I’m not sure where you got your information, but at least the javadoc says that most users will set this to
falseto get Android to send itonDrawevents itself. As for your question about when you should do this, I would say it comes down to why you’re using aSurfaceView.If your view is displaying something dynamic (e.g. for a game or something that has a tight event loop), you’ll want to be controlling exactly when updates happen, especially if you’ll have the information to use one of the more detailed forms of
invalidateto save redrawing the entireView. You won’t want Android to callinvalidatefor you, and that’s why the flag is there.If, on the other hand, you are simply drawing something static, it makes sense to let Android’s UI stack control the invalidations.
By the way,
invalidateonly posts a request to re-draw theView, so be aware of this if you intend to use the event-loop style (onDrawwill be called sometime after you call it).Edit: some clarifications.