Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/308.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 !!
In the previous article, we can already display our surfaceview, button, textview and other components on the same interface. This is basically a success, but as a game developer, if you do not want to intentionally present a video like this (Our surfaceview is in the middle --. really want to play a movie ..), It is absolutely not allowed to play the game as a part of the middle, it must be displayed in full screen, other components are only a supporting role. Next, let's take a look at the reasons for modification and adjustment.
(Figure 1)
Let's see the font we have drawn. It's a tragedy! As long as there is a button, there will be a long bar. Even if we modify the layout color in the button, it will only change the color of the long bar to white. Of course, it looks nice, but it still blocks our font! This is not what we want. The expected effect is as follows:
(Figure 2)
Wahaha, this is the right effect. Our view is full screen, and the component itself will block the content in our view, there will be no more useless strips to block ....
At that time, although I wanted to solve the XML layout problem, I first wanted to add buttons directly in the surfaceview defined in our XML, but the view cannot be added! So there is no way, just think about whether it is a layout problem. After several attempts, we finally succeeded.
<? 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/> <relativelayout <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/> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_alignparentbottom = "true" <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: layout_alignparentbottom = "true" <br/> Android: layout_torightof = "@ ID/button1" <br/> Android: TEXT = "himi button_2" <br/> Android: Id = "@ + ID/button2"/> <br/> <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"/> <br/> </relativelayout> <br/> </linearlayout> <br/>
XML is not modified much, but the previous linear layout is changed to a relative layout. Although the changes are not great, it takes a lot of time to adjust them. In this way, you can add components in your game surfaceview ~~~
The source code has been provided in the previous article. Here we only need to modify the XML. You can download the source code from the previous article and adjust the XML,
(We recommend that you subscribe to this blog, because our update speed is very fast ~ Wahaha)