android - 在 Activity 中使用 GlSurfaceview

标签 android opengl-es android-view glsurfaceview

我有一个 Activity,我已将 Activity 的内容 View 设置为“R.layout.main.xml”。我还有另一个类,其中包含使用 openGL 创建的动画。现在我需要在 Activity 的背景中使用这个动画。

代码是这样的

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

    setContentView(R.layout.main_pixie);

    mGLView = new ClearGLSurfaceView(this);
    setContentView(mGLView);
 }

但是我的应用程序崩溃了..我该如何解决这个问题。

最佳答案

当您第二次调用 setContentView() 时,您将替换第一次设置的内容,只留下背景。崩溃很可能是因为您依赖于已删除的主布局中的元素。

与其调用两次 setContentView(),不如在主布局中包含 GLSurfaceView。以下是如何完成此操作的示例:

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent>
    <your.application.package.ClearGLSurfaceView
         android:layout_width="match_parent"
         android:layout_width="match_parent"/>
    <!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>

然后你可以像往常一样在你的 onCreate() 中加载这个布局(main_pixie_new 指的是上面的 xml,我只是给它起这个名字是为了让事情尽可能清楚):

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

    setContentView(R.layout.main_pixie_new);
 }

关于android - 在 Activity 中使用 GlSurfaceview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851036/

相关文章:

android - 使用索引在没有向上/向下按键的情况下在 gridlayout 中导航

android - 我应该使用哪个 View 来显示仅包含背景颜色的小方 block ,以显示颜色预览?

Android View.getDrawingCache 返回null,只有null

android - fragment 分离方法不会停止相机预览

c - 顶点着色器矩阵乘法不做任何事情

opengl-es - OpenGL ES/实时/显示的任何顶点的位置?

ios - 禁用 opengl es 纹理 ios

android - Flutter Assets 中的多分辨率背景图像

android - 如何使用默认的 Android 可绘制对象

Java/Android : How do I randomly generate 12 objects from an array of 20 objects onto a GridView using an Adapter class?