最後的代碼示例用於啟用 GLSurfaceView 透明度。 但是在 GLSurfaceView 使用透明度之前,考慮下面的否定點。啟用透明度要求你在 GLSurfaceView 上使用 setZOrderOnTop 。 這將阻止你將任何其他視圖( 比如 TextView ) 放在透明 GLSurfaceView 之上。 甚至導航抽屜不能滑動到透明 GLSurfaceView ( 忽略技巧 ) 上面。 無論是否透明,GLSurfaceView 只能存在於其他android視圖之上,也不能在它們之間。
透明度要求你使用 setEGLConfigChooser 和 setFormat,如下面的示例所示。 這意味著,你無法獲得由系統選擇的默認格式,這將是該特定設備最好的設備。 更重要的是,你需要確保設備具有支持的格式,如果預期格式沒有出現在 GSoC 示例中。
為透明 其他選項。在Android中運行 Tracer 為 opengl,將顯示 activity的背景圖像繪製為opengl紋理。 如果可以能的話,不要使 GLSurfaceView 透明,可以將背景作為一個opengl紋理呈現在GLSurfaceView中。
在opengl中,對於alpha混合,表面的alpha通道或者透明度不是預先要求的。 兩者都是獨立的。
如果你的opengl組件在其他視圖之間嵌入,則 TextureView ( 技巧 ) 是不錯的選擇。 這是一個很好的例子,它是一個很好的例子,為 GLSurfaceView 2d 繪圖在Android中。
以下示例帶有布局xml和具有背景的透明 GLSurfaceView 代碼。
使用 green 顏色背景的布局xml文件
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#00FFFF">
android:id="@+id/mySurfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
MainActivity.java 文件從 0.0到 1.0更改 ALPHA 變數以查看表面顏色 red 混合背景 Activity 顏色 greenpackage 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 );
}
});
}
}