Document directory
Surfaceview is a very important drawing container because it can directly obtain image data from hardware interfaces such as memory or DMA. There are many usage of surfaceview on the Internet, and the writing method is also different, for example, surfaceview class or surfaceholder class. callback class, which can be selected based on actual functional needs. I will call the lockcanvas and unlockcanvasandpost of surfaceholder on the general user interface.
The second and third figures are compared. lockcanvas (null. lockcanvas (New rect (oldx, 0, oldx + length, getwindowmanager (). getdefadisplay display (). getheight (), compare the two effects. Because the second graph is painted by the specified rect, the efficiency will be higher than the full control painting of the third graph, and after the screen is cleared (canvas. drawcolor (color. black.
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <linearlayout Android: id = "@ + ID/linearlayout01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <button Android: Id = "@ + ID/button01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "simple painting"> </button> <button Android: Id = "@ + ID/button02" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "timer painting"> </button> </linearlayout> <surfaceview Android: id = "@ + ID/surfaceview01" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> </surfaceview> </linearlayout>
Testsurfaceview. Java
Package COM. testsurfaceview; import Java. util. timer; import Java. util. timertask; import android. app. activity; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rect; import android. OS. bundle; import android. util. log; import android. view. surfaceholder; import android. view. surfaceview; import android. view. view; import android. widget. button; public class testsurfaceview extends activity {/** called when the activity is first created. */button btnsimpledraw, btntimerdraw; surfaceview SFV; surfaceholder SFH; private timer mtimer; private mytimertask mtimertask; int y_axis [], // Save the vertex centery on the Y axis of the sine wave, // center line oldx, Oldy, // The previous XY point currentx; // the point on the X axis currently drawn @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnsimpledraw = (button) This. findviewbyid (R. id. button01); btntimerdraw = (button) This. findviewbyid (R. id. button02); btnsimpledraw. setonclicklistener (New clickevent (); btntimerdraw. setonclicklistener (New clickevent (); SFV = (surfaceview) This. findviewbyid (R. id. surfaceview01); SFH = SFV. getholder (); // the timer for dynamically drawing the sine wave mtimer = new timer (); mtimertask = new mytimertask (); // initialize the Y axis data centery = (getwindowmanager (). getdefadisplay display (). getheight ()-SFV. gettop ()/2; y_axis = new int [getwindowmanager (). getdefadisplay display (). getwidth ()]; for (INT I = 1; I <y_axis.length; I ++) {// calculate the sine wave y_axis [I-1] = centery-(INT) (100 * Math. sin (I * 2 * Math. PI/180);} class clickevent implements view. onclicklistener {@ overridepublic void onclick (view v) {If (V = btnsimpledraw) {simpledraw (Y_axis.length-1); // draw a sine wave directly} else if (V = btntimerdraw) {Oldy = centery; mtimer. schedule (mtimertask, 0, 5); // draw a sine wave dynamically} class mytimertask extends timertask {@ overridepublic void run () {simpledraw (currentx); currentx ++; // forward to if (currentx = y_axis.length-1) {// If the destination is reached, clear the screen and return cleardraw (); currentx = 0; Oldy = centery ;}}} /*** draw the Specified Region */void simpledraw (INT length) {If (length = 0) oldx = 0; canvas = SFH. lockcanvas (New rect (oldx, 0, oldx + length, getwindowmanager (). getdefadisplay display (). getheight (); // key: Get the canvas log. I ("canvas:", String. valueof (oldx) + "," + String. valueof (oldx + length); paint mpaint = new paint (); mpaint. setcolor (color. green); // the paint brush is green mpaint. setstrokewidth (2); // set the paint brush width to int y; For (INT I = oldx + 1; I <length; I ++) {// draw a sine wave y = y_axis [I-1]; canvas. drawline (oldx, Oldy, I, Y, mpaint); oldx = I; Oldy = y;} SFH. unlockcanvasandpost (canvas); // unlock the canvas and submit the painted image} void cleardraw () {canvas = SFH. lockcanvas (null); canvas. drawcolor (color. black); // clear the canvas SFH. unlockcanvasandpost (canvas );}}
Http://www.oschina.net/code/snippet_54100_1423