在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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GLSurfaceView是一种基于OpenGL ES的SurfaceView,它可以用于实现复杂的图形和动画效果,并且支持透明背景。相比于SurfaceViewGLSurfaceView可以更好地处理复杂的图形和动画效果,因为它可以使用OpenGL ES的硬件加速来提高性能和效率。 GLSurfaceView的使用方法与SurfaceView类似,只需要继承GLSurfaceView并实现Renderer接口即可。Renderer接口定义了OpenGL ES的渲染方法,用于在GLSurfaceView中绘制图形和动画效果。 以下是一个简单的GLSurfaceView的实现示例: ```java public class MyGLSurfaceView extends GLSurfaceView implements Renderer { private Square mSquare; public MyGLSurfaceView(Context context) { super(context); setEGLContextClientVersion(2); // 设置OpenGL ES版本为2.0 setRenderer(this); // 设置Renderer setRenderMode(RENDERMODE_CONTINUOUSLY); // 设置渲染模式为连续模式 } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 设置清屏颜色 mSquare = new Square(); // 创建一个正方形对象 } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { GLES20.glViewport(0, 0, width, height); // 设置视口大小 } @Override public void onDrawFrame(GL10 gl) { GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); // 清空颜色缓冲区 mSquare.draw(); // 绘制正方形对象 } } ``` 在上述示例中,Square表示一个正方形对象,它的绘制方法由OpenGL ES实现。通过GLSurfaceView的onSurfaceCreated()、onSurfaceChanged()和onDrawFrame()方法,我们可以在GLSurfaceView中绘制出复杂的图形和动画效果。 总之,GLSurfaceView是一个更加强大和灵活的SurfaceView,可以用于实现复杂的图形和动画效果,并且能够更好地处理透明背景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值