Android:OpenGL-ES 游戏图像 (HUD)

发布于 2024-12-27 05:31:18 字数 94 浏览 2 评论 0原文

我为 Android 制作了一个基本的 3D 游戏,并想添加一个 HUD。我该怎么做?是否可以添加一个位于 3D 游戏之上的布局,或者我必须使用 OpenGL 来完成此操作?

I made a basic 3D Game for Android and want to add a HUD. How should I do this? Is it possible to add a layout which stays on top of the 3D game or I have to do this with OpenGL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光礼记 2025-01-03 05:31:18

可以在 openGL 布局上添加一个或多个布局。为此,您必须从relativelayout开始。它将包含 OpenGL 和 Hud 布局。声明顺序很重要(先声明,先渲染)。要更改 Hud 布局的位置,您可以使用layout_margin 属性,如果是完整覆盖,则可以使用 match_parent。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- START of OpenGL Layout -->
    <LinearLayout
        android:id="@+id/openGLLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp" >
    </LinearLayout>
    <!-- END of OpenGL Layout -->

    <!-- START of HUD Layout -->
    <LinearLayout
        android:id="@+id/hudLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView 
            android:id="@+id/hudText"
            android:text="Hud Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>
    </LinearLayout>
    <!-- END of HUD Layout -->

</RelativeLayout>

在此示例中,OpenGL 布局是用作容器的空 LinearLayout。我通过代码将我自己的 GLSurfaceView 添加到此relativelayout:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)

    setContentView( R.layout.main );
    MyOpenGLSurfaceView view = new MyOpenGLSurfaceView( getApplication() );
    LinearLayout openGLLayout = (LinearLayout)findViewById(R.id.openGLLayout);
    openGLLayout.addView(mView);
}

我主要使用此方法来确认弹出窗口或上下文菜单。这样我就不必使用 openGL 创建自己的按钮。

It is possible to add one or several layouts over the openGL layout. To do this you have to start with a RelativeLayout. It will contain both OpenGL and Hud Layout. The declaration order is important (first declared, first rendered). To change the position of your Hud layout, you can use the layout_margin attributes, or use match_parent if it is a full overlay.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- START of OpenGL Layout -->
    <LinearLayout
        android:id="@+id/openGLLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp" >
    </LinearLayout>
    <!-- END of OpenGL Layout -->

    <!-- START of HUD Layout -->
    <LinearLayout
        android:id="@+id/hudLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp">
        <TextView 
            android:id="@+id/hudText"
            android:text="Hud Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TextView>
    </LinearLayout>
    <!-- END of HUD Layout -->

</RelativeLayout>

In this example, the OpenGL layout is an empy LinearLayout used as a container. I add my own GLSurfaceView to this RelativeLayout by code :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState)

    setContentView( R.layout.main );
    MyOpenGLSurfaceView view = new MyOpenGLSurfaceView( getApplication() );
    LinearLayout openGLLayout = (LinearLayout)findViewById(R.id.openGLLayout);
    openGLLayout.addView(mView);
}

I use this method mainly for confirmation popups or contextual menus. This way I don't have to create my own buttons using openGL.

蒲公英的约定 2025-01-03 05:31:18

典型的解决方案是绘制 3D 场景,然后切换到正交投影并仍然使用 OpenGL 绘制 HUD。将普通组件放置在 OpenGL 画布上可能会产生不可预测的结果(它们可能会闪烁或只是不可见)。

The typical solution is to draw your 3D scene, then switch to an orthographic projection and paint the HUD still using OpenGL. Placing ordinary components on top of an OpenGL canvas will probably have unpredictable results (they might flicker or just simply be invisible).

金橙橙 2025-01-03 05:31:18

我也遇到了同样的问题,并且我已经为此奋斗了将近一年。我没有使用xml布局,而是像这样在java中添加视图:

rel.addView(camPreview, camParams);
rel.addView(glView, glParams);
rel.addView(imageView, imageParams); 

我得到的一个建议是将az索引参数添加到addView调用中,如下所示:

rel.addView(camPreview, 0, camParams);
rel.addView(glView, 1, glParams);
rel.addView(imageView, 2, imageParams);

但这也不起作用..

我什至尝试切换到 xml 布局,但仍然不是我想要的,

最终解决方案是:

glView.setZOrderMediaOverlay(true);
rel.addView(camPreview, camParams);
rel.addView(glView, glParams);
rel.addView(imageView, imageParams);

注意:在实际添加视图之前需要调用 setZOrderMediaOverlay(true)

尝试一下,让我知道这是否适合您。

I was having the same issue and i battled it for almost a year now. I did not use an xml layout, but instead am adding the views in java like so:

rel.addView(camPreview, camParams);
rel.addView(glView, glParams);
rel.addView(imageView, imageParams); 

one suggestion that i got was to add a z index param to the addView calls like so:

rel.addView(camPreview, 0, camParams);
rel.addView(glView, 1, glParams);
rel.addView(imageView, 2, imageParams);

but that didn't work either..

i even tried switching to xml layout but still not what i wanted

in the end the solution was:

glView.setZOrderMediaOverlay(true);
rel.addView(camPreview, camParams);
rel.addView(glView, glParams);
rel.addView(imageView, imageParams);

NOTE: setZOrderMediaOverlay(true) needs to be called before you actually add the view

try that and let me know if that works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文