> First create a view class of your own
--> inherit Surfaceview and implement Surfaceholder.callback interface --> Surfaceview.getholder () to get Surfaceholder object -->surfaceholder.addcallback (callback) Add callback function -->surfaceholder.lockcanvas () Get Canvas objects and lock canvas --> canvas painting -->surfaceholder.unlockcanvasandpost (canvas canvas) end Lock and commit changes , the graphic is displayed. public class Gamesfview extends Surfaceview implements callback{ Private Surfaceholder surfaceholder; private int selectedx = 0; privat e int selectedy = 0; /** * cell width */ Private float width;& nbsp /** * cell height */ Private float height;& nbsp /** *main.xml This Surfaceview class construction method must use the form of two formal parameters */ Public Gamesfview (context context, AttributeSet attrs) { Super (context, Attrs); Surfaceholder = This.getholder (); Get Surfaceholder objects //monitor the life cycle of your surface give Surfaceview the current holder a callback object. Surfaceholder.addcallback (this); Add callback }} implement three callback functions //trigger when surface size changes @ override public void surfacechanged (surfaceholder holder, int format, int width, int height) { & nbsp } //Fires at creation time, typically calling paint threads here. //best to open the drawing thread when surface is created. //Sub-threading drawing cache to surface @Override public void surfacecreated (Surfaceholder Holder) { Draw (); } //Destroy when fired, typically here will draw the thread to stop, release. /preferably destroy the drawing thread @Override public void surfacedestroyed (Surfaceholder) when surface is destroyed Holder) { } Custom Drawing Method /** * Custom drawing methods */ public void Draw () { &N Bsp Synchronized (Surfaceholder) { //Get Canvas object &NBS P Lock the canvas, usually after it is locked, it can be drawn through canvas objects it returns, drawing on it, and so on. Canvas canvas = null; Paint paint = new Paint (); try{ canvas = su Rfaceholder.lockcanvas (); Lock-in canvas (=) nbsp Clear screen draw background Initgameview (canvas, paint); &NBSP ; //Redraw Surfaceview fill new data Inflatenewnum (c Anvas,paint); }CATCH (Exception e) { e.printstacktrace (); &N Bsp }finally{ if (canvas! = null) &NBS P //End lock drawing and submit changes. surfaceholder.unlockcanvasandpost (canvas); //unlock canvas, update } } }& nbsp; it is best to use synchronous locks, and to use try Catch finally for exception trapping. private void Initgameview (Canvas canvas,paint Paint) { //BGBITMAP = Bitmapfactory.decoderesource (Getresources (), R.drawable.background); //init game background INITGAMEBG (canvas,paint); //Initialize game lines Initgameli NE (canvas,paint); /Initialize game data INITFIRSTNUMBER (canvas,paint); } draw lines on the board private void Initgameline ( Canvas canvas,paint Paint) { /Set brush color Paint.setcolor (Color.Black); nbsp //Draw horizontal for (int i=1;i<10;i++) { & nbsp canvas.drawline (0, I*height, getwidth (), i*height, paint); &n Bsp } //Draw vertical line for (int i=1;i<9;i++) { Canvas.drawline (i*height,0, I*height,getwidth (), Pai NT); } //Draw three-way horizontal line paint.setstrokewidth (4); nbsp for (int i=1;i<4;i++) { canvas.drawline (0, I*height*3, GETW Idth (), I*height*3, paint); } //Draw three-way vertical line for (int i =1;i<9;i++) { Canvas.drawline (I*heigh t*3,0, I*height*3,getwidth (), paint); } }
A companion tour, a free dating site: www.jieberu.com
Push family, free tickets, scenic spots: www.tuituizu.com
Android Mini-game----Sudoku (ii)