[Android game development 3] analyzes SurfaceView! Callback and SurfaceHolder !!

Source: Internet
Author: User
Tags drawtext

 

Note: surfaceview does have the onDraw method, but you will not call surfaceview yourself !!!

The ondraw () or draw () in my code is a method defined by myself... Keep calling in the thread. Be sure to pay attention to it !!

Previously, we made comparisons and trade-offs between the view and surfaceview. Finally, we found that surfaceview is more suitable for operation and game development. Let's take a look at the surfaceview structure;

First, run the following code:

/**

*

*/

Package com. himi;

Import android. content. Context;

Import android. graphics. Canvas;

Import android. graphics. Color;

Import android. graphics. Paint;

Import android. view. SurfaceHolder;

Import android. view. SurfaceView;

Import android. view. SurfaceHolder. Callback;

Import android. view. animation. Animation;

/**

* @ Author Himi

*/

Public class MySurfaceView extends SurfaceView implements Callback, Runnable {// Note 1

Private SurfaceHolder sfh;

Private Thread th;

Private Canvas canvas;

Private Paint paint;

Private int ScreenW, ScreenH;

Public MySurfaceView (Context context ){

Super (context );

Th = new Thread (this );

Sfh = this. getHolder ();

Sfh. addCallback (this); // Note 1

Paint = new Paint ();

Paint. setAntiAlias (true );

Paint. setColor (Color. RED );

This. setKeepScreenOn (true); // keep the screen always on

}

@ Override

Public void startAnimation (Animation animation ){

Super. startAnimation (animation );

}

Public void surfaceCreated (SurfaceHolder holder ){

ScreenW = this. getWidth (); // Note 2

ScreenH = this. getHeight ();

Th. start ();

}

Private void draw (){

Try {

Canvas = sfh. lockCanvas (); // get a canvas instance.

Canvas. drawColor (Color. WHITE); // screen Flushing

Canvas. drawText ("Himi", 100,100, paint); // draw text

Canvas. drawText ("this is a simple game framework", 100,130, paint );

} Catch (Exception ex ){

} Finally {// Note 3

If (canvas! = Null)

Sfh. unlockCanvasAndPost (canvas); // submit the painted canvas

}

}

Public void run (){

While (true ){

Draw ();

Try {

Thread. sleep (100 );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

}

Public void surfaceChanged (SurfaceHolder holder, int format, int width,

Int height ){

}

Public void surfaceDestroyed (SurfaceHolder holder ){

// TODO Auto-generated method stub

}

}

The code is simple. We inherit the surfaceview class and use the callback interface and the thread runnable interface. Here I will briefly explain the functions of the Callback interface and the SurfaceHolder class;

// Note 1

Callback interface:

You only need to inherit the SurfaceView class and implement SurfaceHolder. the Callback interface can implement a custom SurfaceView, SurfaceHolder. callback notifies View and SurfaceHolder when the underlying Surface status changes. callback has the following interfaces:

• SurfaceCreated (SurfaceHolder holder): This function is called immediately after the first Surface is created. The program can perform initialization work related to the drawing interface in this function. Generally, the interface is drawn in another thread, so do not draw the Surface in this function.

• SurfaceChanged (SurfaceHolder holder, int format, int width, int height): This function is called when the Surface state (size and format) changes, this function will be called at least once after surfaceCreated is called.

SurfaceHolder class:

It is an interface used to control the surface. It provides an interface to control the surface size, format, and pixels above, that is, to monitor its changes.

The getHolder () function of SurfaceView can obtain the SurfaceHolder object, and the Surface is in the SurfaceHolder object. Although the Surface saves the pixel data of the current window, it does not directly deal with the Surface during use. The Canvas lockCanvas () of SurfaceHolder or Canvas lockCanvas () function to get the Canvas object, and modify the data in the Surface by drawing the content on the Canvas. If the Surface cannot be edited or you have not created it, calling this function will return null. The Surface content in unlockCanvas () and lockCanvas () is not cached, so you need to completely repaint the Surface content, to improve the efficiency, you can call the lockCanvas (Rect rect) function to specify a rect region, so that the content outside the region will be cached. After the lockCanvas function is called to obtain the Canvas, SurfaceView obtains a synchronization lock of the Surface until the unlockCanvasAndPost (Canvas canvas) function is called to release the lock, the synchronization mechanism here ensures that it will not be changed (destroyed or modified) during Surface painting ).

// Note 2

I didn't assign a value to the ScreenW and ScreenH IN THE surfaceview initialization function. Please note that if you call ScreenW = this during initialization. getWidth (); and ScreenH = this. getHeight (); then you will get all the disappointing values as 0, because it is related to the interface Callback interface mechanism. When we inherit the callback interface, we will override its surfaceChanged () surfaceCreated (), surfaceDestroyed (). When surfaceCreated () is executed, the real view is created, that is, the obtained value is 0, it is because the initialization will be executed before the surfaceCreated () method is executed. If the view does not have a value, the height of the screen width must be 0, so pay attention to this point;

// Note 3

Here I try all the draw code, mainly to execute this operation in finally if an exception is thrown in the content of the painting. In this way, when the Code throws an exception, the Surface will not go out of an inconsistent state.

In fact, this is a simple game architecture. Of course, there are still fewer buttons and sound playing. I will write relevant learning articles later. The introduction to surfaceview is about to be introduced here. The understanding here is that I read other people's articles and my own understandings, and of course I may understand some deviations, but I don't think it's too outrageous.

 

This article is from the "Himi" blog, please be sure to keep this source http://xiaominghimi.blog.51cto.com/2614927/605697

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.