mat*_*sta 25 android background opengl-es transparent
我正在构建一个利用OpenGL的Android应用程序.就目前而言,它的背景GLSurfaceView是由我的代码动态生成的,并作为纹理加载并绘制glDrawTexfOES.这是"OK",但我可以简单地更加流畅显示图像自身的表面(无OpenGL的).有什么方法可以让GLSurfaceView透明的背景?我听说过一些传言说这可以做到setEGLConfigChooser,但我还没有找到任何确认.最后,我想带一个面我画并把GLSurfaceView在它达到一个层次分明的效果.
我知道这是一个棘手的,很可能是不可行的,但任何输入都是值得赞赏的.提前致谢.
mat*_*sta 42
我做了一些简单的修改就是为了让它发挥作用.
在我的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);
}
Run Code Online (Sandbox Code Playgroud)
在我的GLSurfaceView:
setEGLConfigChooser(8, 8, 8, 8, 16, 0);
getHolder().setFormat(PixelFormat.TRANSLUCENT);
Run Code Online (Sandbox Code Playgroud)
我使用自己的GLSurfaceView类来显示图表(透明背景/叠加).我的扩展GLSurfaceView通过XML嵌入到弹出窗口中.
<com.dsignmatters.iq_fitfunlite.GLPieChartView
android:id="@+id/gameprogress_chart"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
...
Run Code Online (Sandbox Code Playgroud)
作为活动的一部分,我添加了以下代码:
mGamesuccessPieChart = (GLSurfaceView) gameprogressView.findViewById(R.id.gameprogress_chart);
mGamesuccessPieChart.setZOrderOnTop(true);
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的渲染器类GLPieChartRenderer根本没有调用glClearColor.
最后的代码示例用于启用透明度GLSurfaceView。但在使用透明度之前GLSurfaceView,请考虑以下缺点。
setZOrderOnTopon GLSurfaceView。这将阻止您将任何其他视图(例如TextView)放置在透明之上GLSurfaceView。即使导航抽屉也无法在透明上方滑动GLSurfaceView(忽略技巧)。透明与否,GLSurfaceView只能存在于其他 Android 视图之上或之下,而不能存在于它们之间。setEGLConfigChooser和 ,setFormat如下例所示。这意味着,您无法获得系统选择的最适合该特定设备的默认格式。更重要的是,您需要确保设备具有受支持的格式,并在不存在预期格式(如本 gsoc示例中所示)的情况下选择替代方案。其他选择透明度。
Traceropengl,将显示活动的背景图像被绘制为 opengl 纹理。因此,GLSurfaceView如果可能的话,您也可以在 GLSurfaceView 中将背景渲染为 opengl 纹理,而不是使其透明。TextureView如果你的 opengl 组件要嵌入到其他视图之间,(技巧)是一个很好的选择。这个突破GLSurfaceView游戏是Android 中 2D 绘图的一个很好的例子。GLSurfaceView下面的示例包含布局 xml 和背景透明的代码。
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>
Run Code Online (Sandbox Code Playgroud)MainActivity.java 文件。将变量从 0.0更改ALPHA为 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 );
}
});
}
}
Run Code Online (Sandbox Code Playgroud)