显示文本(如分数),它改变了每帧分数、文本、改变了

2023-09-07 04:08:38 作者:你是我抓不住的梦

有没有简单的方法,在右上角它在实现渲染器类变化对每一帧,在onDrawFrame方法显示简单的白色文本没有任何纹理?它是2D,所以没有必要做任何变革,我相信。我发现很多教程,但他们似乎都太多code太复杂。也许它可以只用code几行来完成?如果这是不可能的,什么是做到这一点,而无需修改太多现有的code中的最简单的方法?请出示完整的code,因为我是一个新手。

Is there any easy way to display simple white text without any textures on the right top corner which changes on each frame in the class which implements Renderer, in onDrawFrame method? It's 2D, so there is no need to make any transformations, I believe. I've found many tutorials, but they all seem just too complex with too much code. Maybe it could be done just with a few lines of code? If that's not possible, what's the easiest way to do it without modifying too much of existing code? Please show complete code as I am a newbie.

推荐答案

渲染是一个OpenGL方法。 OpenGL ES的本身不支持文本。这使得以下选项:

Renderer is an OpenGL method. OpenGL ES doesn't inherently support text. This leaves the following options:

使用一个纹理,使用Android的Canvas类画吧。 使用图书馆。这将极有可能也借鉴到纹理也是如此。 创建多边形文本。

由于您的问题是要求不绘制到纹理的解决方案,然后1和2都出来。而且,由于你想要的东西简单,3也出来了。离开没有好的解决方案。

Since your question is asking for a solution that doesn't draw to a texture, then 1 and 2 are both out. And since you want something simple, 3 is also out. Leaving no good solutions.

要考虑的另一种方法是层上正在使用的GLSurfaceView之上另一种观点。您可以使用RelativeLayout的轻松叠加两种观点,一种是一个TextView,你可以固定在屏幕的右上角。以下伪code:

An alternative to consider is to layer another view on top of the GLSurfaceView you are using. You can use a RelativeLayout to easily stack two views, one being a textview that you can anchor to the top right corner of the screen. Pseudocode below:

<RelativeLayout ... />
    <GLSurfaceView ... />
    <TextView ... />
</RelativeLayout>

该方法具有拉你出去的OpenGL的要求去做文字的利益。我已经在我的几个应用程序这样做成功的。

This approach has the benefit of pulling you out of the OpenGL requirements to do your text. I've done this successfully on a couple of my apps.