Can these two expressions both be true at one time? holder.getSurface().isValid() == true; and canvas = holder.lockCanvas() canvas == null;
1 Answer
From references:
isValid() - Does this object hold a valid surface? Returns true if it holds a physical surface, so lockCanvas() will succeed.
So, if lockCanvas() succeeds it returns a non null canvas.
But you shouldn't access Surface directly (you need synchronization), in surfaceChanged you can just call canvas = holder.lockCanvas() and draw only if canvas is not null.
2 Comments
Victoria Seniuk
Is it meen that I need to implement SurfaceHolder.Callback and check drawing possibility in surfaceCreated method?
ALiGOTec
the SurfaceHolder is initialized asynchronous so, basicaly if you want to draw on it you should have a SurfaceHolder.Callback (to know when it is ready), and the best place to do the drawings is in
surfaceChanged (which is called after surfaceCreated) and where you are provided with width, height and holder.