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, I also wanted to define or use components directly in SurfaceView at the beginning, but the result is definitely not successful, 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. Next 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"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center">
<TextView
Android: id = "@ + id/textview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "This is Himi"
Android: textSize = "32sp"
Android: textColor = "#00FF00"
Android: gravity = "center_horizontal"/>
</LinearLayout>
<FrameLayout
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_weight = "1">
<Com. himi. MySurfaceView android: id = "@ + id/view3d"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
</FrameLayout>
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center">
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Himi Button_1"
Android: id = "@ + id/button1"/>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Himi Button_2"
Android: id = "@ + id/button2"/>
</LinearLayout>
</LinearLayout>
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;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. Window;
Import android. view. WindowManager;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created .*/
Private Button button1, button2;
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. requestWindowFeature (Window. FEATURE_NO_TITLE); // hide the title (Application name)
// This setting must be written before setContentView; otherwise, an exception occurs)
This. getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. main); // you need to display it first, and then retrieve and process its components.
TV = (TextView) findViewById (R. id. textview );
Button1 = (Button) findViewById (R. id. button1 );
Button1.setOnClickListener (this); // here is the listener button, because this class uses the OnClickListener Interface
Button2 = (Button) findViewById (R. id. button2 );
Button2.setOnClickListener (this );
/* You can also use internal classes instead of using interfaces in this class.
* The following is a listener binding method that does not use the OnClickListener interface;
Button2.setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// Process the buttons here
}
});
*/
}
@ Override
Public void onClick (View v ){
If (v = button1 ){
MySurfaceView. button_str = "button 1 triggered ";
TV. setText ("button 1 triggered ");
} Else if (v = button2 ){
MySurfaceView. button_str = "button 2 triggered ";
TV. setText ("button 2 triggered ");
}
}
}
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!
/*
* Public MySurfaceView (Context context) {super (context) ;}// note 1 (Note 1 here. Take a closer look at the description of note 1 below)
*/
Public MySurfaceView (Context 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.
A constructor of a parameter: if it is a new instance, it is certainly 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. During xml initialization, this constructor of two parameters is called, (at that time, this problem had plagued the research for half a day. Finally, with the help of a group of friends, it was discovered that the 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 the button in surfaceview, although the button can be indexed to the button 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.bkjia.com/uploadfile/2011/1113/20111113074403266.rar
This article is from the "Himi" blog