If you want to set a GLSurfaceView to be transparent, the solution is quite simple but takes quite a bit of trial and error
to get there. Here is my trial and error to save you doing it too.
In the android documentation it states that it is possible to make a GLSurfaceView transparent by a call to
getHolder().setFormat(PixelFormat.TRANSLUCENT);
This might have been true once but on my device (HTC Desire, with API 8) this can cause the rendering to just look corrupted. It basically appears to be two badly coloured images of what I’m drawing.
Most of the material I found on the web states that you need to use setEGLConfigChooser to select the correct surface format that includes transparency. I guess calling setFormat(PixelFormat.TRANSLUCENT) isn’t doing anything to the OpenGL surface.
Common consent appears to say that the desired format is as follows:default
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
This gives 32 bits, 8 bits per channel including alpha although the default is 16 bits without alpha (RGB656)
However just calling this will cause my phone to crash. This is because the format is now out of sync with whatever getHolder().setFormat(PixelFormat.TRANSLUCENT) gave us. So instead I needed to call
glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);
Finally, the last and least documented requirement is to call
glSurfaceView.setZOrderOnTop(true);
You can easily check this works for you by modifying the SurfaceViewOverlay example in the APIDemos that are supplied with the SDK.
Before it calls
glSurfaceView.setRenderer(new CubeRenderer(false));
Add the lines
glSurfaceView.setZOrderOnTop(true);
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);
So, the cubes will appear on top of the hideme buttons when you make them visible.
I hope this helps anyone struggling with a transparent GLSurfaceView.
Filed under: Programming | Tagged: Android, Programming | Comments Off