Android games-Sudoku (2)

Source: Internet
Author: User
Tags android games

Android games-Sudoku (2)
> First, create a View class --> inherit SurfaceView and implement SurfaceHolder. callback interface --> SurfaceView. getHolder () obtains the SurfaceHolder object --> SurfaceHolder. addCallback (callback) adds a callback function --> SurfaceHolder. lockCanvas () Get the Canvas object and lock 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 S UrfaceHolder 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 // The @ 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 drawing 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 }} it is best to use a synchronization lock and use try catch finally for exception capture. 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 the private void initGameLine (Canvas canvas, Paint paint) on the checker {// set 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 );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.