[英]Android OpenGL ES Transparent Background
我正在構建一個利用OpenGL的Android應用。 就目前而言, GLSurfaceView
的背景由我的代碼動態生成,並作為紋理加載並使用glDrawTexfOES
繪制。 沒關系,但是我可以簡單地將圖像更平滑地顯示在其自身的表面上(無需OpenGL)。 有什么方法可以使GLSurfaceView
的背景透明嗎? 我聽說有傳言說可以使用setEGLConfigChooser
來完成,但是我沒有找到任何確認。 最終,我想取一個我要繪制的表面,並在其上放置GLSurfaceView
以獲得分層效果。
我知道這是一個棘手的問題,而且很可能是不可行的,但是任何輸入都值得贊賞。 提前致謝。
我做了一些簡單的更改才能使它生效。
在我的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);
您的GLSurfaceView
還需要setZOrderOnTop(true);
我使用自己的GLSurfaceView類顯示圖表(透明背景/疊加層)。 我擴展的GLSurfaceView通過XML嵌入到一個彈出窗口中。
<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);
最后但並非最不重要的一點是,我的GLSurfaceView看起來像這樣:
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
。
最后的代碼示例用於啟用GLSurfaceView
透明度。 但是在將透明性與GLSurfaceView
一起使用之前,請考慮以下負面因素。
setZOrderOnTop
上使用GLSurfaceView
。 這將阻止您將其他任何視圖(例如TextView
)放置在透明GLSurfaceView
頂部。 甚至導航抽屜也不能在透明的GLSurfaceView
上方滑動(忽略技巧 )。 GLSurfaceView
透明, GLSurfaceView
只能存在於其他android視圖之上或之下,而不能存在於它們之間。 setEGLConfigChooser
和setFormat
如以下示例所示。 這意味着,您將無法獲得系統選擇的默認格式,而該格式最適合該特定設備。 更重要的是,您將需要確保設備具有受支持的格式,並在不存在此gsoc 示例中所期望的格式的情況下選擇其他格式。 其他透明度選項 。
Tracer
,將顯示活動的背景圖像繪制為opengl紋理。 因此,如果可能的話, GLSurfaceView
使GLSurfaceView
透明之外,還可以在GLSurfaceView中將背景渲染為opengl紋理。 TextureView
( 技巧 )是一個很好的選擇。 這個突破游戲是Android中GLSurfaceView
2d繪圖的一個很好的例子。 下面的示例具有布局xml和用於帶有背景的透明GLSurfaceView
代碼。
具有green
背景的布局xml文件
<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>
MainActivity.java文件。 將ALPHA
變量從0.0更改為1.0,以查看表面顏色為red
與背景活動顏色為green
混合
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 ); } }); } }
您應該確保活動背景是透明的!
如果活動的背景是不透明的,默認情況下為白色,那么即使內容視圖(glsurfaceview)是半透明的,它也會將活動的白色背景顯示為背景。
您可以在宣告活動的Android.manifest中設置活動背景的透明度,請參閱官方API演示TranslucentGLSurfaceViewActivity以獲得幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.