The obvious difference between surfaceview and view is that the surface does not need to update the view through a thread, but the lockcanvas method must be used to lock the canvas before painting, and
Go to the canvas, draw the canvas, and use the unlockcanvasandpost method to unlock the canvas. The surfaceview class handles the same event as the View class.
Class for drawing the interface:
1. package COM. yarin. android. examples_05_02; <br/> 2. <br/> 3. import android. content. context; <br/> 4. import android. graphics. canvas; <br/> 5. import android. graphics. color; <br/> 6. import android. graphics. paint; <br/> 7. import android. view. surfaceholder; <br/> 8. import android. view. surfaceview; <br/> 9. <br/> 10. public class gamesurfaceview extends surfaceview <br/> 11. <br/> 12. implements surfaceholder. callback, runnable <br/> 13. {<br/> 14. // control loop <br/> 15. boolean mbloop = false; <br/> 16. <br/> 17. // define the surfaceholder object <br/> 18. surfaceholder msurfaceholder = NULL; <br/> 19. int micount = 0; <br/> 20. int y = 50; <br/> 21. <br/> 22. <br/> 23. public gamesurfaceview (context) <br/> 24. {<br/> 25. super (context); <br/> 26. <br/> 27. // instantiate surfaceholder <br/> 28. msurfaceholder = This. getholder (); <br/> 29. <br/> 30. // Add callback <br/> 31. msurfaceholder. addcallback (this); <br/> 32. this. setfocusable (true); <br/> 33. <br/> 34. mbloop = true; <br/> 35 .} <br/> 36. <br/> 37. <br/> 38. // triggered when the surface size changes <br/> 39. public void surfacechanged (surfaceholder holder, int format, int width, int height) <br/> 40. {<br/> 41. <br/> 42 .} <br/> 43. <br/> 44. // triggered when the surface is created <br/> 45. public void surfacecreated (surfaceholder holder) <br/> 46. {<br/> 47. // enable the drawing thread <br/> 48. new thread (this ). start (); <br/> 49 .} <br/> 50. <br/> 51. // triggered when the surface is destroyed <br/> 52. public void surfacedestroyed (surfaceholder holder) <br/> 53. {<br/> 54. // stop the loop <br/> 55. mbloop = false; <br/> 56 .} <br/> 57. <br/> 58. // drawing cycle <br/> 59. public void run () <br/> 60. {<br/> 61. while (mbloop) <br/> 62. {<br/> 63. try <br/> 64. {<br/> 65. thread. sleep (1, 200); <br/> 66 .} <br/> 67. catch (exception e) <br/> 68. {<br/> 69. <br/> 70 .} <br/> 71. synchronized (msurfaceholder) <br/> 72. {<br/> 73. draw (); <br/> 74 .} <br/> 75. <br/> 76 .} <br/> 77 .} <br/> 78. <br/> 79. // drawing method <br/> 80. public void draw () <br/> 81. {<br/> 82. // lock the canvas and get canvas <br/> 83. canvas canvas = msurfaceholder. lockcanvas (); <br/> 84. <br/> 85. if (msurfaceholder = NULL | canvas = NULL) <br/> 86. {<br/> 87. return; <br/> 88 .} <br/> 89. <br/> 90. if (micount <100) <br/> 91. {<br/> 92. micount ++; <br/> 93 .} <br/> 94. else <br/> 95. {<br/> 96. micount = 0; <br/> 97 .} <br/> 98. // drawing <br/> 99. paint mpaint = new paint (); <br/> 100. mpaint. setantialias (true); <br/> 101. mpaint. setcolor (color. black); <br/> 102. // draw a rectangle-Clear the screen <br/> 103. canvas. drawrect (0, 0,320,480, mpaint); <br/> 104. switch (micount % 4) <br/> 105. {<br/> 106. case 0: <br/> 107. mpaint. setcolor (color. blue); <br/> 108. break; <br/> 109. case 1: <br/> 110. mpaint. setcolor (color. green); <br/> 111. break; <br/> 112. case 2: <br/> 113. mpaint. setcolor (color. red); <br/> 114. break; <br/> 115. case 3: <br/> 116. mpaint. setcolor (color. yellow); <br/> 117. break; <br/> 118. default: <br/> 119. mpaint. setcolor (color. whitelist); <br/> 120. break; <br/> 121 .} <br/> 122. <br/> 123. canvas. drawcircle (320-25)/2, Y, 50, mpaint); <br/> 124. // unlock after painting. After painting, it must be unlocked before it can be displayed <br/> 125. msurfaceholder. unlockcanvasandpost (canvas); <br/> 126 .} <br/> 127 .}