我开始了一个Android OpenGL应用程序,我有以下类:
class A extends Activity
class B extends GlSurfaceView implements Renderer
当A类的onCreate被调用时,它创建了一个B类的对象并调用:
setContentView(Bobject)
迄今为止,它运作良好,我花了几天时间来处理它。
现在我想要在我的应用程序中添加按钮,并找到了SurfaceViewOverlay示例。它使用一些XML来创建视图层次结构。我想要创建与此非常相似的东西,所以我只是剪切和粘贴了XML代码:
<android.opengl.GLSurfaceView android:id="@+id/glsurfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout android:id="@+id/hidecontainer"
android:orientation="vertical"
android:visibility="gone"
android:background="@drawable/translucent_background"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
现在考虑一下我最初的类层次结构,我该如何初始化我的视图?在类A的onCreate()方法中应该写什么?
我尝试使用了以下代码:
Bobject = new B(this);
GLSurfaceView glSurfaceView =
(GLSurfaceView) findViewById(R.id.glsurfaceview);
glSurfaceView.setRenderer(Bobject);
它能够在屏幕上绘制按钮和GL视图,但是GL视图无法接收来自点击的任何输入。
这很可能是因为Bobject的onTouchEvent()没有被调用,因为它只被用作:
Renderer
而不是a:
glSurfaceView
对象。
与上面的代码不同,我真正想要的是让Bobject替换glSurfaceView。但我不知道该怎么做。当我执行findViewById()时,似乎glSurfaceView已经被创建了。我如何要求它使用类型为B的对象作为GL视图?
对任何新手错误感到抱歉。完全是Android的新手。
编辑:我还尝试了以下操作:
我还尝试了以下操作: 在我的XML文件中,我将GLSurfaceView更改为:
<com.bla.bla.B
android:id="@+id/glsurfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在我的 A 类构造函数中,我调用了:
// This returns null
Bobject = (B) findViewById(R.id.glsurfaceview);
// And this suspends the application. :(
setContentView(R.layout.surface_view_overlay);
我应该如何在XML文件/Activity中使用扩展glSurfaceView的自定义类?