使用XML / Java的Android的glSurfaceView与覆盖 [英] Android glSurfaceView with overlay using XML/Java
问题描述
我开始一个Android OpenGL应用程序,我有以下类:
I started an Android OpenGL application and I have the following classes:
class A extends Activity
class B extends GlSurfaceView implements Renderer
当A类的onCreate被调用时,它会创建类B类的一个对象,然后调用:
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.
现在我想按钮添加到我的应用程序,发现SurfaceViewOverlay例子。使用一些XML创建视图层次结构。我想创造的东西非常相似,我简单地剪切和粘贴XML code:
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">
...
现在牢记我原来的类层次结构,我将如何初始化我的意见?我应该怎么写我的A级中的onCreate()?
Now keeping in mind my original class hierarchy, how would I initialize my views? What should I write in onCreate() of my class A?
我试着使用:
Bobject = new B(this);
GLSurfaceView glSurfaceView =
(GLSurfaceView) findViewById(R.id.glsurfaceview);
glSurfaceView.setRenderer(Bobject);
和它绘制按钮和屏幕上的GL看法,但GL的观点是无法采取从水龙头/点击任何输入。
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.
这是可能的,因为Bobject的的onTouchEvent()将不会调用,因为它只是被用来作为:
This is likely because Bobject's onTouchEvent() is not called as it is only being used as a:
Renderer
而不是
glSurfaceView
对象。
,我真正想要的是有Bobject更换glSurfaceView。但我不知道该怎么做。当我做findViewById(),它似乎像glSurfaceView已经为现在创建。我怎么能要求其使用B型的对象为GL看法?
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?
对不起任何新手的错误。全新到Android。
Sorry about any newbie mistakes. Completely new to Android.
编辑:我也尝试过这样的:
I also tried this:
我也试过如下: 在我的XML文件,我改变了GLSurfaceView为:
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);
我应该如何使用,在我的XML文件/活动延伸glSurfaceView我的自定义类?
How should I use my custom class that extends glSurfaceView in my XML file/Activity?
推荐答案
好了,这样做的正确的方法是让在B类,需要在构造函数中:
Okay, so the proper way of doing this is to have a constructor in the B class which takes in:
B(Context context, AttributeSet attrs)
{
super(context, attrs);
}
而在XML布局,使用这样的:
And in the XML layout, use this:
<com.bla.bla.B
android:id="@+id/glsurfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
缺少在我的code。该事情是构造签名。
The thing that was missing in my code was the constructor signature.
这篇关于使用XML / Java的Android的glSurfaceView与覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!