Android games, android
Segment 2
Android games-Sudoku (2)
From the previous article, we will summarize the usage of surfaceView.
--> First, create a View class.
--> Inherit SurfaceView and implement the SurfaceHolder. Callback Interface
--> SurfaceView. getHolder () obtains the SurfaceHolder object.
--> SurfaceHolder. addCallback (callback) Add a callback function
--> SurfaceHolder. lockCanvas () obtains the Canvas object and locks the Canvas.
--> Canvas painting
--> SurfaceHolder. unlockCanvasAndPost (Canvas canvas) ends locking and submits changes to display the image.
Public class GameSFView extends SurfaceView implements Callback {private SurfaceHolder surfaceHolder; private int selectedX = 0; private int selectedY = 0;/*** cell width */private float width; /*** cell height */private float height;/*** main. the surfaceview class constructor referenced in xml must use two form parameters */public GameSFView (Context context, AttributeSet attrs) {super (context, attrs); surfaceHolder = this. getHolder (); // get Su RfaceHolder object // listens to the life cycle of the Surface to give the current owner of SurfaceView a callback object. SurfaceHolder. addCallback (this); // Add callback}
}
Implement three callback Functions
// When the surface size changes, @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}// triggered during creation, generally, the drawing thread is called here. // It is best to enable the drawing thread when the Surface is created. // The Sub-thread plot is cached to the surface @ Override public void surfaceCreated (SurfaceHolder holder) {draw () ;}// triggered upon destruction, generally, the drawing thread is stopped and released here. // It is best to destroy the drawing thread @ Override public void surfaceDestroyed (SurfaceHolder holder) {} when the Surface is destroyed ){}
Custom Plotting Method
/*** Custom Drawing Method */public void draw () {synchronized (surfaceHolder) {// get the Canvas object // lock the Canvas, generally, after locking the image, you can use its returned Canvas object to draw images on it. Canvas canvas = null; Paint paint = new Paint (); try {canvas = surfaceHolder. lockCanvas (); // lock the Canvas // clear the screen painting background initGameView (canvas, paint); // repaint surfaceview to fill in the new data inflateNewNum (canvas, paint );} catch (Exception e) {e. printStackTrace ();} finally {if (canvas! = Null) // Stop locking the drawing and submit the changes. SurfaceHolder. unlockCanvasAndPost (canvas); // unlock Canvas, update }}}
We recommend that you use the synchronization lock and try catch finally to catch exceptions.
Private void initGameView (Canvas canvas, Paint paint) {// bgBitmap = BitmapFactory. decodeResource (getResources (), R. drawable. background); // initialize the game background initGameBg (canvas, paint); // initialize the game line initGameLine (canvas, paint); // initialize the game data initFirstNumber (canvas, paint );}
Draw lines on the board
Private void initGameLine (Canvas canvas, Paint paint) {// you can specify the paint color. setColor (Color. BLACK); // draw a horizontal line for (int I = 1; I <10; I ++) {canvas. drawLine (0, I * height, getWidth (), I * height, paint);} // draw a vertical line for (int I = 1; I <9; I ++) {canvas. drawLine (I * height, 0, I * height, getWidth (), paint);} // draw three horizontal and coarse line paints. setStrokeWidth (4); for (int I = 1; I <4; I ++) {canvas. drawLine (0, I * height * 3, getWidth (), I * height * 3, paint);} // draw three vertical and coarse lines for (int I = 1; I <9; I ++) {canvas. drawLine (I * height * 3, 0, I * height * 3, getWidth (), paint );}}View Code
After painting, draw a rough line.
The effect is as follows,
The effects of the Board are as follows. We will talk about the initialization data and buttons in the figure below.
Here we only paste some code to illustrate how to use surfaceview. The entire source code will be attached.