When surfaceview is used for game development, it is essential to use surfaceholder to process the effects and animations drawn on its canvas. Used to control the surface, size, pixels, and so on.
Abstract interface to someone holding a display surface. allows you to control the surface size and format,
Edit the pixels in the surface, and monitor changes to the surface. This interface is typically available
Through the surfaceview class.
Pay special attention to the following functions:
Abstract void addcallback (surfaceholder. Callback callback );
// Send a callback object to the current owner of surfaceview.
Abstract canvas lockcanvas ();
// Lock the canvas. After locking the canvas, you can use its returned canvas object to draw a picture on it.
Abstract canvas lockcanvas (rect dirty );
// Draw a picture in a certain area of the canvas. After the painting, the unlockcanvasandpost file is called to change the display content.
// For games with relatively high memory requirements, you do not need to redraw pixels in other regions outside dirty to increase the speed.
Abstract void unlockcanvasandpost (canvas );
// Stop the locked drawing and submit the changes.
Example:
Class drawthread extends thread {private surfaceholder holder; private Boolean running = true; protected drawthread (surfaceholder holder) {This. holder = holder;} protected void dostop () {running = false;} public void run () {canvas c = NULL; while (running) {c = holder. lockcanvas (null); // lock the entire canvas. We recommend that you do not set the parameter to null try {synchronized (holder) {bgrid when the memory requirement is high. drawgrid (c); // draw the grid bboom in the game. drawbooms ( C, booms); // draw the bomb bfairy in the game. drawfairy (c); // draw the main character in the game. // The content is in the Z axis, and the latter will overwrite the previous one. } Catch (exception ex) {}finally {holder. unlockcanvasandpost (c); // update the Screen Content }}}};
From http://blog.csdn.net/py890000/article/details/5439233