//////// Inherit the class public class paintview extends surfaceview implements runnable and surfaceholder of surfaceview. callback {private float MX; private float my; private paint mpaint = NULL; private path mpath = NULL; // Boolean mloop = true; surfaceholder msurfaceholder = NULL; canvas mcanvas; Public paintview (context, attributeset ARR) {super (context, arr); msurfaceholder = This. getholder (); // Obtain holder msurfaceholder. addcallback (this); msurfaceholder. setformat (pixelformat. opaque); // opaque // msurfaceholder. setformat (pixelformat. rgb_565); // msurfaceholder. setformat (pixelformat. rgba_8888); this. setfocusable (true); // setzorderontop (true); // put it to the top-level mpaint = new paint (); mpath = New Path (); mpaint. setantialias (true); mpaint. setstyle (style. stroke); mpaint. setstrokewidth (5); mpaint. setcolor (mpaint Color); mpaint. setstrokecap (paint. cap. round); // round head mpaint. setdither (true); // remove the pull to smooth the screen into the mpaint. setstrokejoin (paint. join. round); // method of combination, smooth} @ 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 new thread (this ). start (); // start thread} @ Override public void surfacedestroyed (surfaceholder holder) {// todo auto-generated method stub mloop = false; // end thread} @ override public void run () {While (mloop = true) {draw (); try {thread. sleep (100);} catch (interruptedexception e) {e. printstacktrace () ;}}// drawing private void draw () {try {mcanvas = msurfaceholder. lockcanvas (); mcanvas. drawpath (mpath, mpaint); //} catch (exception E) {} Finally {If (mcanvas! = NULL) {msurfaceholder. unlockcanvasandpost (mcanvas) ;}}@ override public Boolean ontouchevent (motionevent event) {Switch (event. getaction () {Case motionevent. action_down: Touchdown (event); break; Case motionevent. action_move: touchmove (event);} // update the invalidate (); Return true;} // call private void touchdown (motionevent event) when the screen is clicked) {// hide the previous drawing // mpath. reset (); // float x = event. getx (); float y = event. gety (); MX = x; my = y; // the start point of mpath. moveTo (x, y);} // call private void touchmove (motionevent event) {final float x = event when the finger slides on the screen. getx (); Final float y = event. gety (); Final float previusx = Mx; Final float previusy = My; Final float dx = math. ABS (X-previusx); Final float DY = math. ABS (Y-previusy); // when the distance between two points is greater than or equal to 3, The beiser curve is generated if (dx> = 3 | dy> = 3) {// set the start point and end point to half float Cx = (x + previusx)/2; float Cy = (Y + previusy)/2; // implement a smooth curve by secondary besell. previusx, previusy is the operation point, CX, and Cy are the end point mpath. quadto (previusx, previusy, CX, CY); // The coordinate value of the first end call is used as the initial coordinate value MX = x of the second call; my = y ;}/// eraser private void seteraser () {mpaint = new paint (); mpath = New Path (); mpaint. setantialias (true); mpaint. setstyle (style. stroke); mpaint. setstrokewidth (50); // mpaint. setcolor (mpaintcolor); mpaint. setstrokecap (paint. cap. round); // round head mpaint. setdither (true); // remove the pull to smooth the screen into the mpaint. setstrokejoin (paint. join. round); // combine to smooth the mpaint. setalpha (0); // mpaint. setxfermode (New porterduxfermode (porterduff. mode. clear) ;}//// // display the preceding view setcontentview (New paintview (this) in the activity ));
Write and erase freely in surfaceview