在GLSurfaceView之上添加SurfaceView层

1. 首先,初始化系统:

[java]  view plain copy
  1. rivate void sys_init(){  
  2.     // 去掉标题栏  
  3.     requestWindowFeature(Window.FEATURE_NO_TITLE);  
  4.       
  5.     // 全屏  
  6.     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,    
  7.                   WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  8.       
  9.     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);     //强制为横屏  
  10.     //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    //强制为竖屏  
  11.       
  12.     // 获取屏幕的分辨率  
  13.     screenWidth  = getWindowManager().getDefaultDisplay().getWidth();       // 屏幕宽(像素,如:480px)     
  14.     screenHeight = getWindowManager().getDefaultDisplay().getHeight();      // 屏幕高(像素,如:800p)  
  15.     normalLayerZ = -((float)screenHeight/2.0f);   
  16.     System.out.println("screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);          
  17. }  


 

2.主函数入口的onCreate函数实现:

[java]  view plain copy
  1. MainMenu mainMenu;  
  2. MySurfaceView mGLSurfaceView;  
  3.   
  4. LinearLayout imageButtonLinearLayout;  
  5. ImageButton mytestButton;  
  6. ImageButton mytestButton2;  
  7.   
  8. @Override  
  9. protected void onCreate(Bundle savedInstanceState) {  
  10.     super.onCreate(savedInstanceState);  
  11.   
  12.     sys_init();  
  13.     setContentView(R.layout.main);  
  14.       
  15.     /* -------------------- Start add ImageButtons------------------------------------- */  
  16.     Context context = this.getApplicationContext();  
  17.     imageButtonLinearLayout = new LinearLayout(this);  
  18.     imageButtonLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
  19.     imageButtonLinearLayout.setOrientation(LinearLayout.VERTICAL);  
  20.     // add imagebuttons  
  21.     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  22.     LinearLayout imagebuttonLinearLayout = (LinearLayout) inflater.inflate(R.layout.imagebutton, imageButtonLinearLayout, false);  
  23.     imageButtonLinearLayout.addView(imagebuttonLinearLayout);  
  24.     /* -------------------- End add ImageButtons------------------------------------- */  
  25.       
  26.     mGLSurfaceView=new MySurfaceView(MainActivity.this);  
  27.     mGLSurfaceView.requestFocus();//获取焦点  
  28.     mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控  
  29.     gn_DB_type=dCimovDbType_Singer;  
  30.       
  31.     mGLSurfaceView.setZOrderOnTop(true);    // 置到Top层  
  32.     mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);  // 设置背景为透明  
  33.       
  34.     // 添加3D SurfaceView (GLSurfaceView)  
  35.     setContentView(mGLSurfaceView);  
  36.       
  37.     // 添加imageButtonLinearLayout (SurfaceView)  
  38.     addContentView(imageButtonLinearLayout, new LayoutParams(LayoutParams.FILL_PARENT,  
  39.             LayoutParams.FILL_PARENT));  
  40.       
  41.     // 添加SurfaceView上的按钮响应事件  
  42.     mytestButton=(ImageButton)findViewById(R.id.button1);  
  43.     mytestButton.setOnClickListener(CimovSysBtnClickListen);   
  44.       
  45.     mytestButton2=(ImageButton)findViewById(R.id.button2);  
  46.     mytestButton2.setOnClickListener(CimovSysBtnClickListen);   
  47. }  

 


3.main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/cimov"  
  7.     android:gravity="center"  
  8.     >  
  9.   
  10.       
  11. </LinearLayout>  

 


4.imagebutton.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal" android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content" android:layout_weight="2">  
  5.   
  6.     <EditText  
  7.         android:id="@+id/setup_hostip_edit"  
  8.         android:paddingLeft="100dp"  
  9.         android:layout_width="100dp"   
  10.         android:layout_height="50dp"  
  11.         android:layout_gravity="bottom"  
  12.         android:textSize="20sp"  
  13.         android:paddingBottom="8dp"  
  14.         android:singleLine="true"  
  15.         android:editable="true"        
  16.         />  
  17.       
  18.     <ImageButton android:id="@+id/button1" android:src="@drawable/icon"  
  19.         android:paddingLeft="100dp"  
  20.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  21.         android:layout_gravity="bottom" android:background="@null" />  
  22.     <ImageButton android:id="@+id/button2" android:src="@drawable/icon"  
  23.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  24.         android:layout_gravity="bottom" android:background="@null" />  
  25.     <ImageButton android:id="@+id/button3" android:src="@drawable/icon"  
  26.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  27.         android:layout_gravity="bottom" android:background="@null" />  
  28.     <ImageButton android:id="@+id/button4" android:src="@drawable/icon"  
  29.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  30.         android:layout_gravity="bottom" android:background="@null" />  
  31.     <Button android:id="@+id/button5" android:src="@drawable/icon"  
  32.         android:layout_width="wrap_content" android:layout_height="wrap_content"  
  33.         android:layout_gravity="bottom" android:background="@null" />  
  34.           
  35. </LinearLayout>  


 

5.拓展说明:

  在某些资料资料中,有使用FrameLayer来布局的,先添加GLSurfaceView,再添加SurfaceView,也是一样的。


http://blog.csdn.net/ypist/article/details/8810335


你可能感兴趣的:(在GLSurfaceView之上添加SurfaceView层)