Java 将GLSurfaceView类与android xml布局一起使用

Java 将GLSurfaceView类与android xml布局一起使用,java,android,android-layout,opengl-es,glsurfaceview,Java,Android,Android Layout,Opengl Es,Glsurfaceview,我想利用android的xml布局。我把一个GLSURFACHEVIEW放在一个框架布局中,与线性布局一起使用,就像这样 <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <android.opengl.GLSurfaceView android:id="

我想利用android的xml布局。我把一个GLSURFACHEVIEW放在一个框架布局中,与线性布局一起使用,就像这样

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

<android.opengl.GLSurfaceView android:id="@+id/surfaceviewclass"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</FrameLayout>

<LinearLayout android:id="@+id/gamecontrolslayout"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="5"
          android:background="@drawable/backdrop"> 
//some layout stuff

</LinearLayout>
<LinearLayout>

在onCreate()中

我如何调用我的GLSURFACHEVIEW,这样我就可以利用像这样的xml布局并引用我自己的GLSURFACHEVIEW类(下面是引用我自己的GLSURFACHEVIEW类的代码)


有没有办法把这两者结合起来?我想这样做,因为我的glSurfaceView类中有很多东西在进行,比如文件加载和触摸事件。只有我刚刚考虑过实现这个新布局

只需在xml中引用您自己的类(带有完整的packagename),就像引用android.opengl.GLSurfaceView一样。确保子类实现了正确的构造函数,并将上下文和属性传递给父类:

public MyGLSurfaceView(Context context, AttributeSet attrs)
{
   super(context, attrs);
然后,您可以使用findViewById获取它:

MySurfaceView glSurfaceView = 
             (MySurfaceView)findViewById(R.id.surfaceviewclass);

这应该可以解决问题。

如果一切都正确,就像您在xml布局中编写的那样,那么GLSURFACHEVIEW类的完整路径:(和类名)

只有当类GLSurfaceView写在自己的文件中时,它才起作用。 在舒尔的此文件中,需要正确书写施工人员

我读过关于xml引用的一个构造函数和一个用于类间通信的构造函数。 如果编写正确,可以在GLSurfaceView中找到xml引用的构造函数和类间通信的构造函数 . GLSurfaceView是设置渲染器的位置, 在xml构造函数中设置它,必须是唯一的方法,它可以正常工作。(如图1所示)

xml构造函数:

公共MyGLSurfaceView(Context-Context,AttributeSet-attrs){super(Context,attrs); setEGLContextClientVersion(2);
渲染器=新渲染器(上下文); 设置渲染器(渲染器)

如果你是这些民族中的一部分, 谁不能得到SurfaceView的工作,在xml布局或,谁买了这本书从一个压力开始的3D游戏开发。 不要生气或伤害自己。 在第44-45页,应将其写入一个文件中。 像在我的回答中一样,在自己的文件中编写GLSURFACHEVIEW。 渲染器是自己的文件,其中: 在表面上创建, 表面上发生了变化, onDrawFrame..可以找到
MainActivity

My Surface View没有属性集,这很重要吗?上面的implementend说它在扩展类时有问题。错误来自xml文件
公共类MyGLSurfaceView扩展了GLSurfaceView{
和构造函数
public MyGLSurfaceView(上下文上下文){super(上下文)
但是现在我对我的surface视图(即:
glSurfaceView.requestRender
)所做的任何调用都已停止工作=(奇怪的是,现在一切都正常了。结果是,除了需要使用
glSurfaceView=new MyGLSurfaceView获取glSurfaceView外,你已经完成了你所说的一切
然后将xml文件设置为内容视图多么烦人。请更新您的答案以适合您。如果您真的学习了android的gl es,我会购买《开始与…3D》一书中的印刷品-Robert Chin,到您桌旁的那本书,请看新的bosten。
public MyGLSurfaceView(Context context, AttributeSet attrs)
{
   super(context, attrs);
MySurfaceView glSurfaceView = 
             (MySurfaceView)findViewById(R.id.surfaceviewclass);