Android glSurfaceView 使用 XML/Java 进行覆盖
我启动了一个 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() 未被调用,因为它仅用作 a:
Renderer
而不是 a:
glSurfaceView
对象。
我真正想要的是让 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 的自定义类?
I started an Android OpenGL application and I have the following classes:
class A extends Activity
class B extends GlSurfaceView implements Renderer
When class A's onCreate is called, it creates an object of type class B and calls:
setContentView(Bobject)
So far it works and I spent a few days on it.
Now I wanted to add buttons to my application and found the SurfaceViewOverlay example. That uses some XML to create a view hierarchy. I wanted to create something very similar to I simply cut and paste the XML code:
<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">
...
Now keeping in mind my original class hierarchy, how would I initialize my views? What should I write in onCreate() of my class A?
I tried using:
Bobject = new B(this);
GLSurfaceView glSurfaceView =
(GLSurfaceView) findViewById(R.id.glsurfaceview);
glSurfaceView.setRenderer(Bobject);
And it does draw the buttons and the GL view on the screen, but the GL view is unable to take in any input from taps/clicks.
This is likely because Bobject's onTouchEvent() is not called as it is only being used as a:
Renderer
and not a:
glSurfaceView
object.
Instead of the above code, what I really want is to have Bobject replace glSurfaceView. But I don't know how to do that. When I do findViewById(), it seems like the glSurfaceView is already created for now. How can I ask it to use an object of type B for the GL view?
Sorry about any newbie mistakes. Completely new to Android.
EDIT: I also tried this:
I also tried the following:
In my XML file, I changed the GLSurfaceView to:
<com.bla.bla.B
android:id="@+id/glsurfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And in my A class constructor I am calling:
// This returns null
Bobject = (B) findViewById(R.id.glsurfaceview);
// And this suspends the application. :(
setContentView(R.layout.surface_view_overlay);
How should I use my custom class that extends glSurfaceView in my XML file/Activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,所以正确的方法是在 B 类中有一个构造函数,它接受:
在 XML 布局中,使用这个:
我的代码中缺少的是构造函数签名。
Okay, so the proper way of doing this is to have a constructor in the B class which takes in:
And in the XML layout, use this:
The thing that was missing in my code was the constructor signature.
解决此问题的另一种方法是将您的视图附加到 Activity A。因此,您可以使用 setContentView(A object) 而不是使用 setContentView(B object),并且在“A XML”文件内是 GLsurfaceView 视图:
然后您可以引用 Activity A 中的 B 对象,因为当您将 ContentView 设置为 A 而不是 B 时,它会膨胀:
老实说,我不确定如何让它完全按照您想要的方式工作,但就您而言,我不是确保它最终会有所作为。话又说回来,我也还是个新手。我有兴趣了解如何以另一种方式做到这一点,因为我今天问了类似的问题。
An alternate way to solve this, is to leave your views attached to Activity A. So instead of using setContentView(B object) you use setContentView(A object) and inside your "A XML" file, is the GLsurfaceView view:
You can then refer to your B object in the Activity A, because it was inflated when you setContentView to A, instead of B:
I'm honestly not sure how to get it to work exactly the way you want, but in your case I'm not sure it ultimately makes a difference. Then again, I'm still a novice too. I'd be interested to learn how to do this in an alternate fashion as I asked a similar question just today.