1. Concept
Surfaceview is a subclass of the View class. It is a very important drawing view to obtain image data directly from memory or DMA and other hardware interfaces. Its feature is that it can be drawn to the screen outside the main thread. In this way, the main thread blocking can be avoided when the drawing task is heavy, thus improving the response speed of the program. Surfaceview is often used in game development. The background, characters, and animations in the game should be drawn in the canvas whenever possible.
2. Implementation Method
1) implementation steps
A. inherit surfaceview
B. Implement the surfaceholder. Callback Interface
2) method to be rewritten
Public void surfacechanged (surfaceholder holder, int format, int width, int height) {}// triggered when the surface size changes
Public void surfacecreated (surfaceholder holder) {}// triggered during creation. Generally, the drawing thread is called here.
Public void surfacedestroyed (surfaceholder holder) {}// triggered when the image is destroyed. Generally, the painting thread is stopped and released here.
3) surfaceholder
Surfaceholder, the surface controller, used to manipulate the surface. It processes effects and animations drawn on its canvas, controls the surface, size, pixels, and so on.
Several Methods to note:
Abstract void addcallback (surfaceholder. Callback callback); // return a callback object to the current owner of surfaceview. Abstract canvas lockcanvas (); // lock the canvas. Generally, after locking the canvas, you can use its returned canvas object to draw images on it. Abstract canvas lockcanvas (rect dirty); // lock a certain area of the canvas for drawing, etc. .. because after drawing, the following unlockcanvasandpost 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 and lock the painting and submit the changes.
4) summarize the entire process
Inherit surfaceview and implement surfaceholder. callback interface ----> surfaceview. getholder () obtains the surfaceholder object ----> surfaceholder. addcallback (callback) adds the callback function ----> surfaceholder. lockcanvas () Get the canvas object and lock the canvas ----> canvas painting ----> surfaceholder. unlockcanvasandpost (canvas) ends the lock drawing, and submits changes to display the drawing.
The following is a complete case:
Public class viewtest extends activity {@ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (New myview (this);} // class inside the view myview extends surfaceview implements surfaceholder. callback {private surfaceholder holder; private mythread; Public myview (context) {super (context); // todo auto-generated constructor stub holde R = This. getholder (); holder. addcallback (this); mythread = new mythread (holder); // create a drawing thread} @ override public void surfacechanged (surfaceholder holder, int format, int width, int height) {// todo auto-generated method stub} @ override public void surfacecreated (surfaceholder holder) {// todo auto-generated method stub mythread. isrun = true; mythread. start () ;}@ override public void surfacedestro Yed (surfaceholder holder) {// todo auto-generated method stub mythread. isrun = false ;}}// Internal Thread class mythread extends thread {private surfaceholder holder; Public Boolean isrun; Public mythread (surfaceholder holder) {This. holder = holder; isrun = true ;}@ override public void run () {int COUNT = 0; while (isrun) {canvas c = NULL; try {synchronized (holder) {c = holder. lockcanvas (); // lock the canvas, generally After locking, you can use the canvas object that it returns to draw images on it. C. drawcolor (color. black); // set the canvas background color paint P = new paint (); // create a paint brush p. setcolor (color. white); rect r = new rect (100, 50,300,250); C. drawrect (R, P); C. drawtext ("this is the" + (count ++) + "seconds", 100,310, P); thread. sleep (1000); // The sleep duration is 1 second} catch (exception e) {e. printstacktrace ();} finally {If (C! = NULL) {holder. unlockcanvasandpost (c); // end the lock drawing and submit the changes. }}}}}}