How to make libGDX background transparent in Android?

154 Views Asked by At

Our Android project now needs to generate a game in activity with transparent background.

I notice that libGDX use GLSurfaceView to draw, and I know it's able to make GLSurfaceView background transparent by setZOrderOnTop(true) and getHolder().setFormat(PixelFormat.TRANSLUCENT)

But it is not working in libGDX. I guess it influences by the function Gdx.gl.glClearColor() which always invokes in render(), and it seems like the alpha channel invalid in Gdx.gl.glClearColor(). When I set transparent color Gdx.gl.glClearColor(0, 0, 0, 0), the background will always be black. And If I delete this, the render result comes to error.

So I wonder is there a way to make libGDX background transparent?

Thank you all.

1

There are 1 best solutions below

0
EmMper On BEST ANSWER

I finally made libGDX background transparent by the following code:

// add this to your AndroidApplication
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.a = 8; // default is 0
View view = initializeForView(new YourApplicationAdapter(), config);
if (view instanceof SurfaceView) {
    SurfaceView sv = (SurfaceView) view;
    sv.setZOrderOnTop(true);
    sv.getHolder().setFormat(PixelFormat.TRANSLUCENT);
}
setContentView(view);
// add this to your ApplicationAdapter
public void render() {
    // other codes
    ScreenUtils.clear(0, 0, 0, 0);
    // other codes
}

And I find a good way to discuss what you want about libGDX by Discord.