简体   繁体   中英

Thread or Runnable when using Canvas and SurfaceView in Android

I am interested in creating a 2D drawing application for Android using touch input. I will be using a SurfaceView to accomplish this as it is the most efficient next to OpenGL, but for this application, it should now be an issue.

After reading the documentation and overview ( http://developer.android.com/guide/topics/graphics/2d-graphics.html ) of SurfaceView and Canvas drawing, it seems they recommend creating an inner class which extends Thread to handle the work for drawing.

This makes sense of course as to not lock up UI and offload the work to a secondary thread. However, I am unsure why they do not use an external class which extends Thread , or even better, why not use a Runnable that is external?

Is there a benefit to leaving this as an inner class? And is there a benefit to extending Thread as opposed to implementing Runnable for Canvas drawing?

Thanks!

Well in their scenario they got one thing right and one thing wrong IMO:

  1. The decision to make the thread class an inner class is ok, because that class is only required in that particular part of the code, so no need to expose it to the outside.

  2. I don't really agree with extending Thread . The best practice is to implement Runnable and override the run method. This is because you can use that Runnable in several different ways, like starting a normal thread, queuing it up in a thread pool, etc. It also allows you to extend a different class if you need, since Runnable is just an interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM