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

时间:2012-01-12 20:57:05

标签: java android opengl-es

我为Android制作了一个基本的3D游戏,想要添加一个HUD。我该怎么做?是否可以添加一个位于3D游戏顶部的布局,或者我必须使用OpenGL执行此操作?

3 个答案:

答案 0 :(得分:2)

可以在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布局是用作容器的empy 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创建自己的按钮了。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

我遇到了同样的问题,现在我和它打了差不多一年了。我没有使用xml布局,而是在java中添加视图,如下所示:

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

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

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)

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

相关问题