【问题标题】:Using GLSurfaceView class with android xml layouts将 GLSurfaceView 类与 android xml 布局一起使用
【发布时间】:2011-12-15 21:36:22
【问题描述】:

我想使用 android xml 布局。我已将 glSurfaceView 放在框架布局中,以与线性布局结合使用,如下所示...

<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>

然后我这样称呼我的布局

setContentView(R.layout.main);
    GLSurfaceView glSurfaceView = (GLSurfaceView)findViewById(R.id.surfaceviewclass);

在 onCreate();

如何调用我的 glSurfaceView 以便我可以使用这样的 xml 布局并引用我自己的 GLSurfaceView 类(下面是引用我自己的 GLSurfaceView 类的代码)...

glSurfaceView = new MyGLSurfaceView(this);
    setContentView(glSurfaceView);

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

【问题讨论】:

    标签: java android android-layout opengl-es glsurfaceview


    【解决方案1】:

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

    public MyGLSurfaceView(Context context, AttributeSet attrs)
    {
       super(context, attrs);
    

    然后您可以使用 findViewById 获取它:

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

    这应该可以解决问题。

    【讨论】:

    • 我的 Surface View 没有属性集应该有关系吗?
    • 刚刚实现上面说它在膨胀类时遇到了麻烦。错误来自xml文件
    • public class MyGLSurfaceView extends GLSurfaceView { 和构造函数public MyGLSurfaceView(Context context){ super(context);
    • 但是现在我对表面视图的任何调用(即:glSurfaceView.requestRender)都已停止工作 =(
    • 奇怪现在一切正常。结果你做了你所说的一切,除了你需要用glSurfaceView = new MyGLSurfaceView(this);获取glSurfaceView,然后将xml文件设置为内​​容视图多么烦人。你能更新你的答案以适应吗?
    【解决方案2】:

    如果一切正确,就像你在 xml 布局中写的那样,Glsurfaceview-class 的完整路径:(和类名)

    只有当类 GLSurfaceView 写在自己的文件中时它才有效。 在舒尔的这个文件中,需要正确编写构造函数。

    我阅读了关于 xml-refer 的 1 个构造函数和一个用于类之间通信的构造函数。 用于 xml-refer 的构造函数,以及用于类之间通信的构造函数,如果正确编写,可以在 GLSurfaceView 中找到 . GLSurfaceView,是你设置渲染器的地方, 在xml构造函数中设置它,必须是唯一的方法,它工作正常。 (如答案1所示)

    xml 构造函数:

    public MyGLSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); setEGLContextClientVersion(2);
    渲染器 = 新渲染器(上下文); setRenderer(渲染器);

    如果你们这些人中的一些人, 谁不能在 xml-layout 矿石中获得 SurfaceView 工作,谁从 Apress 购买这本书 - 开始 3D - 游戏开发。 不要生气或伤害自己。 在第 44-45 页将其写入一个文件中。 像我的回答一样,在自己的文件中编写 GLSurfaceView。 渲染器是自己的文件,其中: onSurfaceCreated, onSurfaceChanged, onDrawFrame..可以找到 和 MainActivity

    【讨论】:

    • 如果你真的学习了 android 的 gl es,我会购买“Beginning And...3D”一书中的印刷品 - Robert Chin,放在你的桌子旁边,看看新的 bosten。跨度>
    猜你喜欢
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2014-09-26
    相关资源
    最近更新 更多