我启动了一个 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
而不是:
glSurfaceView
object.
我真正想要的是让 Bobject 替换 glSurfaceView,而不是上面的代码。但我不知道该怎么做。当我执行 findViewById() 时,看起来 glSurfaceView 现在已经创建了。我如何要求它为 GL 视图使用 B 类型的对象?
对于任何新手错误,我们深表歉意。对于 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 文件/活动中使用扩展 glSurfaceView 的自定义类?