[Android game development 6] add components to surfaceview !!!! And interact with each other !!!!

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/306.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 surfaceview will not be called by itself !!!

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 !!



Yesterday's Christmas, I didn't go out, but I spent a whole day entangled in how to add components to surfaceview, such as adding common buttons, textview and so on. At the beginning, I also wanted to find some information from the Internet to see if there was any reference, but I found that some children's shoes were still confused about this and I was also looking for answers. So, here we will share with you the results of the Christmas Day;


1. because our surfaceview is a view, it is actually a view for the added components. If we just want to add a view component to surfaceview, it is actually a wrong idea, of course, at first I wanted to define or use components directly in surfaceview, but the result was definitely unsuccessful,Because view cannot be added!


2. since the first line is definitely wrong, we should think of putting our surfaceview and components in a layout, after all, our surfaceview is also a view that is placed in our layout together with other components. In this way, we will certainly be able to add components to surfaceview. First,




We can see that the white area in the middle is our surfaceview. The top is the component textview, and the bottom is the button and right. What we need is this effect! Instead of switching multiple activities in the previous article, they are all in one interface. Wow, haha. Okay. Let's take a look at the code below:


Put the XML code first:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <linearlayout <br/> Android: orientation = "horizontal" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "center"> </P> <p> <textview <br/> Android: Id = "@ + ID/textview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: text = "this is himi" <br/> Android: textsize = "32sp" <br/> Android: textcolor = "#00ff00" <br/> Android: gravity = "center_horizontal"/> </P> <p> </linearlayout> </P> <p> <framelayout <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_weight = "1"> <br/> <COM. himi. mysurfaceview Android: Id = "@ + ID/view3d" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"/> <br/> </framelayout> </P> <p> <linearlayout <br/> Android: orientation = "horizontal" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "center"> <br/> <button </P> <p> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "himi button_1" <br/> Android: id = "@ + ID/button1"/> </P> <p> <button Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "himi button_2" <br/> Android: id = "@ + ID/button2"/> <br/> </linearlayout> <br/> 

The above code is very simple. It is a set of layout methods and attributes and display methods of each component. Of course, it mainly depends on how to register surfaceview in XML, each component has an ID to be used for subsequent interactive data. Because we need to operate on each component, the ID is indexed here to retrieve its objects from the R file.


So, we have defined the XML. Let's take a look at how to implement it in the Code. Here we will first talk about the code in the activity class:




Package COM. himi; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. window; <br/> Import android. view. windowmanager; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. textview; <br/> public class mainactivity extends activity implements onclicklistener {<br/>/** called wh En the activity is first created. */<br/> private button button1, button2; <br/> private textview TV; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> This. requestwindowfeature (window. feature_no_title); // hide the title (Application name) <br/> // This setting must be written before setcontentview; otherwise, an exception occurs. <br/> This. getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, <br /> Windowmanager. layoutparams. flag_fullscreen); <br/> setcontentview (R. layout. main); // display it first, and then retrieve and process its components <br/> TV = (textview) findviewbyid (R. id. textview); <br/> button1 = (button) findviewbyid (R. id. button1); <br/> button1.setonclicklistener (this); // here is the listener button, because this class uses the onclicklistener interface <br/> button2 = (button) findviewbyid (R. id. button2); <br/> button2.setonclicklistener (this); <br/>/* in fact, you do not need to use the interface of this class. You can use the internal class to complete. <Br/> * the following method does not use the onclicklistener interface. <br/> button2.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view V) {<br/> // click here to process the button operation </P> <p >}< br/> }); <br/> */<br/>}< br/> @ override <br/> Public void onclick (view v) {<br/> If (V = button1) {<br/> mysurfaceview. button_str = "button 1 triggered"; <br/> TV. settext ("button 1 triggered"); <br/>} else if (V = button2) {<br/> mysurfaceview. button_str = "button 2 triggered"; <br/> TV. settext ("button 2 triggered"); <br/>}< br/>} 


This remark is added after the code, mysurfaceview. button_str: This is a static variable defined in surfaceview to interact with data. In this case, we need to look at our surfaceview. When registering in XML, what should we pay attention, I spent most of my time here !!! It must be noted that this is also the most important step in surfaceview to display the component completion.


First analysis:

1. the creation and implementation of the surfaceview class are the same as before. How can I write the surfaceview class! Be sure to pay attention to the constructor!



/* <Br/> * Public mysurfaceview (context) {super (context);} // note 1 (Note 1 here. Take a closer look at the description of note 1 below) <br/> */<br/> Public mysurfaceview (context, attributeset attrs) {// Note 1} 


Note 1: There are two constructor functions. Of course, we can use either of them, but at this point we need to determine which one we want to use.

Constructor of a parameter:If it is a new instance, there is no problem, but we want to display other components while displaying surfaceview, therefore, the custom surfaceview is also registered in main -- XML as a component,When registered in XML, you must use this constructor with two parameters in surfaceview., This constructor of the two parameters will be called during XML initialization. (at that time, this problem had plagued the research for half a day. Finally, with the help of a group of friends, it was found that this problem had occurred) in the method that contains two constructor parameters, the second parameter refers to some attributes of the custom component, just like the length and width. You can pass these attributes to the component!



In surfaceview, the component is displayed in the end. In retrospect, there are three steps: 1. place Our surfaceview as a component view and other components in the layout. Of course, you can define the layout mode and display mode as you like! 2. In surfaceview, we must use the constructor of two constructor functions! Yes! There is a difference here. The other is how to handle it, that is, the constructor has changed 3. interactive Data is bound to the activity by pressing the buttons. do not bind the view to our surfaceview. Otherwise, an error is returned --,


Here we will explain why we need to bind buttons in the activity instead of binding them in our surfaceview:


 In fact, we can get the button through the R. ID index based on the ID when the button is defined in XML, which can be obtained in activity or surfaceview,! If you write a button in surfaceview, an error is reported;

When we define surfaceview and component buttons and textview in XML, they are at the same level !! Instead of including a button in surfaceview

So although the button can be indexed by ID in surfaceview, the button cannot be found during binding, only our activitysetcontentview (R. layout. main); The displayed button, so it can only be bound to the activity that displays it. Note the following;


The source code is as follows:


Source code: http://www.himigame.com/android-game/306.html


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


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.