简体   繁体   English

如何使Android中的libGDX背景透明?

[英]How to make libGDX background transparent in Android?

Our Android project now needs to generate a game in activity with transparent background.我们的 Android 项目现在需要在活动中生成具有透明背景的游戏。

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)我注意到 libGDX 使用 GLSurfaceView 进行绘制,我知道它能够通过setZOrderOnTop(true)getHolder().setFormat(PixelFormat.TRANSLUCENT)使 GLSurfaceView 背景透明

But it is not working in libGDX.但它在 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() .我猜它会受到 function Gdx.gl.glClearColor()的影响,它总是在render()中调用,并且似乎 alpha 通道在Gdx.gl.glClearColor()中无效。 When I set transparent color Gdx.gl.glClearColor(0, 0, 0, 0) , the background will always be black.当我设置透明色Gdx.gl.glClearColor(0, 0, 0, 0)时,背景将始终为黑色。 And If I delete this, the render result comes to error.如果我删除它,渲染结果就会出错。

So I wonder is there a way to make libGDX background transparent?所以我想知道有没有办法让 libGDX 背景透明?

Thank you all.谢谢你们。

I finally made libGDX background transparent by the following code:我终于通过以下代码使 libGDX 背景透明:

// 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 .我找到了一个很好的方法来讨论您对 Discord 的libGDX的看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM