Drawing on the canvas of a SurfaceView
Even after inheriting from View
for custom drawing code, we are limited to Android's determination and frequency to actually redraw the view.
How to do it...
Drawing on a View
instance requires that the drawing occurs within the UI update cycles as well as on the UI thread. This provides limitations that can be avoided by drawing on a SurfaceView
instance from another thread:
- In order to draw on a
SurfaceView
instance, we need aSurface
instance to draw on. Thus, we need to know when the surface is created and destroyed:public class MainActivity : Activity, ISurfaceHolderCallback { private ISurfaceHolder surfaceHolder; public void SurfaceChanged( ISurfaceHolder holder, Format format, int width, int height) { } public void SurfaceCreated(ISurfaceHolder holder) { surfaceHolder = holder; } public void SurfaceDestroyed(ISurfaceHolder holder) { surfaceHolder = null } }
- Then, we need to get hold of the
ISurfaceHolder
instance...