Segment 2
Android Mini-game----Sudoku (ii)
From the previous article, we summarize the usage of Surfaceview
--First create your own view class
--Inherit Surfaceview and implement Surfaceholder.callback interface
--Surfaceview.getholder () Get Surfaceholder Object
-->surfaceholder.addcallback (callback) Add callback function
-->surfaceholder.lockcanvas () Get the canvas object and lock the canvas
--Canvas painting
-->surfaceholder.unlockcanvasandpost (canvas canvas) ends the lock and commits the change to display the graphic.
Public classGamesfviewextendsSurfaceviewImplementscallback{PrivateSurfaceholder Surfaceholder; Private intSelectedx = 0; Private intSelectedy = 0; /*** Width of cell*/ Private floatwidth; /*** Height of cell*/ Private floatheight; /**this Surfaceview class construction method referenced in *main.xml must use the form of two formal parameters*/ PublicGamesfview (Context context, AttributeSet attrs) {Super(context, attrs);Surfaceholder= This. Getholder ();//Get Surfaceholder Object//Monitor the life cycle of your surface to give Surfaceview the current holder a callback object. Surfaceholder.addcallback ( This);//Add callback }
}
Implementation of three callback functions
//fires when the size of your surface changes@Override Public voidSurfacechanged (Surfaceholder holder,intFormatintWidthintheight) { } //fired at creation time, typically where the drawing thread is called. //It's a good idea to turn on the drawing thread when surface is created. //child thread drawing cache to surface@Override Public voidsurfacecreated (Surfaceholder holder) {Draw (); } //fires when destroyed, typically where the drawing thread is stopped and released. //It's best to destroy the drawing thread when your surface is destroyed.@Override Public voidsurfacedestroyed (Surfaceholder holder) {}
Custom Drawing methods
/*** Custom Drawing method*/ Public voidDraw () {synchronized(surfaceholder) {//get the Canvas object//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=NewPaint (); Try{Canvas= Surfaceholder.lockcanvas ();//Lock Canvas//Clean screen drawing backgroundInitgameview (canvas, paint); //Redraw Surfaceview Fill the new dataInflatenewnum (Canvas,paint); }Catch(Exception e) {e.printstacktrace (); }finally{ if(Canvas! =NULL) //end the lock drawing and commit the change. Surfaceholder.unlockcanvasandpost (canvas);//unlock canvas, update } } }
It is best to use a synchronous lock and catch the exception using try Catch finally.
Private void Initgameview (Canvas canvas,paint Paint) { //Bgbitmap = Bitmapfactory.decoderesource ( Getresources (), r.drawable.background); // initializing the game background INITGAMEBG (canvas,paint); // Initialize the game line Initgameline (canvas,paint); // Initializing game Data Initfirstnumber (canvas,paint); }
Draw lines on a board
Private voidinitgameline (Canvas canvas,paint Paint) {//Set Brush ColorPaint.setcolor (Color.Black); //Draw Horizontal Line for(inti=1;i<10;i++) {Canvas.drawline (0, I*height, getwidth (), i*height, paint); } //draw a vertical line for(inti=1;i<9;i++) {Canvas.drawline (i*height,0, i*height,getwidth (), paint); } //draw a three-way horizontal linePaint.setstrokewidth (4); for(inti=1;i<4;i++) {Canvas.drawline (0, I*height*3, getwidth (), i*height*3, paint); } //draw three vertical thick lines for(inti=1;i<9;i++) {Canvas.drawline (i*height*3,0, I*height*3, GetWidth (), paint); } }View Code
Draw a thick line after drawing it vertically.
Effects such as,
The effect of the board as above, as to the initialization of the data and buttons we say next.
This is just a piece of code that shows how to use Surfaceview. Finally, the entire source code will be attached.
Android Mini-game----Sudoku (ii)