Handling the multi-touch in a surface view

In the previous post Handling the multi-touch in a view I’ve used a class that extends a View to show the circles where the screen is touched, here I display the same example but using a SurfaceView.

The SurfaceView is more suited than the View when the updates are frequent beacause it uses a separate thread from the UI thread.
The files in this example and in that one in Handling the multi-touch in a view are equal or almost except replaced by then I don’t write here these files and their comments and screenshot.

This class handles the drawing of the background and circles in the method surfaceCreated().
Note this class implements the interface SurfaceHolder.Callback to receive information about changes to the surface.
In the constructor TouchSurfaceView at the lines 27 and 28 I initialize SurfaceHolder and I implement the Callback.
In the method surfaceCreated() and inside the thread mDrawingThread I perfomr the following steps:

  1. line 57: canvas = mSurfaceHolder.lockCanvas()
    I get the surface where I draw
  2. lines 60-81
    I draw the background and the circles
  3. line 82: mSurfaceHolder.unlockCanvasAndPost(canvas)
    the drawing is completed and the changes to the surface are shown

You can download the example here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.