[Android game development 3] analyzes surfaceview! Callback and surfaceholder !!

Source: Internet
Author: User


Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/296.html

Many kids shoes say that after my code is run, clicking home or back will cause a program exception. If you have encountered this, you certainly haven't carefully read the himi blog, in article 19th, himi specifically wrote about the causes and solutions of these errors. I have added my remarks on this blog, and my provincial children's shoes are always confused. Please click here to contact us for further reading:

[Android game development 19th] (required) surfaceview Running Mechanism explanation-analyze back and home buttons and switch to the background to handle exceptions!

 

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:

 

 

 

/** <Br/> */<br/> package COM. himi; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> Import android. view. surfaceholder. callback; <br/> Import android. view. animation. animation; <br/>/** <br/> * @ author himi <br/> */ <Br/> public class mysurfaceview extends surfaceview implements callback, runnable {// Note 1 <br/> private surfaceholder SFH; <br/> private thread th; <br/> private canvas; <br/> private paint; <br/> private int screenw, screenh; <br/> Public mysurfaceview (context) {<br/> super (context); <br/> TH = new thread (this); <br/> SFH = This. getholder (); <br/> SFH. addcallback (this );// Note 1 <br/> paint = new paint (); <br/> paint. setantialias (true); <br/> paint. setcolor (color. red); <br/> This. setkeepscreenon (true); // keep the screen always on <br/>}< br/> @ override <br/> Public void startanimation (animation) {<br/> super. startanimation (animation); <br/>}< br/> Public void surfacecreated (surfaceholder holder) {<br/> screenw = This. getwidth (); // Note 2 <br/> screenh = This. getheight (); <br/> Th. start (); <br/>}< br/> private void draw () {<br/> try {<br/> canvas = SFH. lockcanvas (); // get a canvas instance <br/> canvas. drawcolor (color. white); // screen flushing <br/> canvas. drawtext ("himi", 100,100, paint); // draw text <br/> canvas. drawtext ("this is a simple game framework", 100,130, paint); <br/>} catch (exception ex) {<br/>}finally {// Note 3 <br/> If (canvas! = NULL) <br/> SFH. unlockcanvasandpost (canvas); // submit the painted canvas <br/>}< br/> Public void run () {<br/> while (true) {<br/> draw (); <br/> try {<br/> thread. sleep (100); <br/>} catch (interruptedexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> Public void surfacechanged (surfaceholder holder, int format, int width, <br/> int height) {<br/>}< br/> Public void surfacedestroyed (surfaceholder holder) {<br/> // todo auto-generated method stub <br/>}< br/>}

 

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,IntFormat,IntWidth,IntHeight): This function is called when the surface state (size and format) changes. After surfacecreated is called, the function is called at least once.

 

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) 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) 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.

 

(We recommend that you subscribe to this blog, because our update speed is very fast ~ Wahaha)

 

 


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.