代码之家  ›  专栏  ›  技术社区  ›  mattbasta

Android OpenGL ES透明背景

  •  25
  • mattbasta  · 技术社区  · 15 年前

    GLSurfaceView 由我的代码动态生成,作为纹理加载并使用 glDrawTexfOES GLSURFACHEVIEW 透明的?我听到一些谣言说这可以用 setEGLConfigChooser ,但我还没有找到任何证实。最后,我想画一个曲面,然后把 GLSURFACHEVIEW 在其上实现分层效果。

    我知道这是一个棘手的问题,很可能是不可行的,但任何意见都是非常感谢的。提前谢谢。

    5 回复  |  直到 11 年前
        1
  •  44
  •   mattbasta    15 年前

    我只是做了一些简单的改变来让它工作。

    在我的 GLSurfaceView.Renderer :

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
                GL10.GL_FASTEST);
    
         gl.glClearColor(0,0,0,0);
         gl.glEnable(GL10.GL_CULL_FACE);
         gl.glShadeModel(GL10.GL_SMOOTH);
         gl.glEnable(GL10.GL_DEPTH_TEST);
    }
    

    GLSurfaceView :

    setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);
    
        2
  •  22
  •   Napalm    13 年前

    GLSurfaceView 还需要 setZOrderOnTop(true);

        3
  •  9
  •   Ziem    8 年前

    我使用自己的GLSurfaceView类来显示图表(透明背景/覆盖)。 我的扩展GLSURFACHEVIEW通过XML嵌入到popover窗口中。

    <com.dsignmatters.iq_fitfunlite.GLPieChartView          
        android:id="@+id/gameprogress_chart"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        ...
    

    mGamesuccessPieChart = (GLSurfaceView) gameprogressView.findViewById(R.id.gameprogress_chart);
    mGamesuccessPieChart.setZOrderOnTop(true);
    

    最后但并非最不重要的是,我的GLSURFACHEVIEW如下所示:

    public class GLPieChartView extends GLSurfaceView {
        public GLPieChartView(Context context) {
            super(context);
            initGL();
        }
    
        public GLPieChartView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initGL();
        }
    
        void initGL() {
            setEGLContextClientVersion(2);
            setEGLConfigChooser(8,8,8,8,16,0);
            setRenderer(new GLPieChartRenderer());
            setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
            getHolder().setFormat(PixelFormat.TRANSLUCENT);     
        } 
    }
    

    我的渲染器类 GLPieChartRenderer 不打电话 glClearColor 完全

        4
  •  4
  •   Community CDub    7 年前

    最后的代码示例用于启用透明度 GLSurfaceView GLSURFACHEVIEW

    1. 启用透明度要求您使用 setZOrderOnTop 在…上 GLSURFACHEVIEW TextView )在你透明的 GLSURFACHEVIEW . 即使是导航抽屉也无法在透明屏幕上滑动 GLSURFACHEVIEW tricks ). 透明与否,, GLSURFACHEVIEW 只能存在于其他android视图的上方或下方,而不能位于它们之间。
    2. 透明度要求您使用 setEGLConfigChooser setFormat example .

    .

    1. Tracer GLSURFACHEVIEW 透明,如果可能的话,您还可以在GLSURFACHEVIEW中将背景渲染为opengl纹理。
    2. 此外,曲面上的alpha通道或透明度不是opengl中alpha混合的先决条件。两者都是独立的。
    3. TextureView ( trick breakout 游戏就是一个很好的例子

    有背景。

    1. 使用 green 彩色背景

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"
      android:background="#00FFFF">
      
      <android.opengl.GLSurfaceView
          android:id="@+id/mySurfaceView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      
      </RelativeLayout>
      
    2. MainActivity.java文件。改变 ALPHA red 与背景活动颜色混合 绿色

      package com.example.transparentsurfaceview;
      
      import android.app.Activity;
      import android.graphics.PixelFormat;
      import android.opengl.GLES20;
      import android.opengl.GLSurfaceView;
      import android.os.Bundle;
      
      import javax.microedition.khronos.egl.EGLConfig;
      import javax.microedition.khronos.opengles.GL10;
      
      public class MainActivity extends Activity {
      
          private GLSurfaceView mSurfaceView;
          private static float ALPHA = 0.5f;
          private static float RED   = 1.0f;
          private static float GREEN = 0.0f;
          private static float BLUE  = 0.0f;
      
          protected void onCreate( Bundle savedInstanceState ) {
              super.onCreate( savedInstanceState );
              setContentView( R.layout.activity_main );
              mSurfaceView = (GLSurfaceView)findViewById( R.id.mySurfaceView );
      
              mSurfaceView.setEGLContextClientVersion( 2 );
              mSurfaceView.setZOrderOnTop( true );
              mSurfaceView.setEGLConfigChooser( 8, 8, 8, 8, 16, 0 );
              mSurfaceView.getHolder().setFormat( PixelFormat.RGBA_8888 );
      
              mSurfaceView.setRenderer( new GLSurfaceView.Renderer() {
                  public void onSurfaceCreated( GL10 gl10, EGLConfig eglConfig ) {
                      GLES20.glClearColor( RED, GREEN, BLUE, ALPHA );
                  }
      
                  public void onSurfaceChanged( GL10 gl10, int i, int i2 ) {}
      
                  public void onDrawFrame( GL10 gl10 ) {
                      GLES20.glClear( GLES20.GL_COLOR_BUFFER_BIT );
                  }
              });
          }
      }
      
        5
  •  1
  •   bismack163    11 年前

    你应该确保活动背景是透明的!

    如果活动的背景是不透明的(默认情况下为白色),则即使内容视图(GLSURFACHEVIEW)是半透明的,它也会将活动的白色背景显示为其背景。

    您可以在声明的Android.manifest中设置活动背景透明度,请参阅官方API演示半透明GLSurfaceDeviceActivity以获取帮助