【问题标题】:Android glSurfaceView with overlay using XML/Java使用 XML/Java 覆盖的 Android glSurfaceView
【发布时间】:2011-05-06 11:05:22
【问题描述】:

我启动了一个 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

对象。

我真正想要的不是上面的代码,而是让 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 的自定义类?

【问题讨论】:

  • 我遇到了同样的问题,找不到演示这个东西的整个应用程序。谁能展示一些示例应用程序?我似乎无法从任何地方的 android 示例中找到这个著名的 SurfaceOverlayView 示例。提前致谢。

标签: android button surfaceview glsurfaceview


【解决方案1】:

好的,所以正确的做法是在 B 类中有一个构造函数,它接受:

B(Context context, AttributeSet attrs)
{
  super(context, attrs);
}

在 XML 布局中,使用这个:

<com.bla.bla.B
        android:id="@+id/glsurfaceview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

我的代码中缺少的是构造函数签名。

【讨论】:

  • 添加相同的东西我得到“构造函数对象(上下文,属性集)未定义”你能指导我吗?
  • 我已经在Renderer类中添加了。
【解决方案2】:

解决此问题的另一种方法是将视图附加到 Activity A。因此,您使用 setContentView(A object) 而不是使用 setContentView(A object),并且在“A XML”文件中,是 GLsurfaceView 视图:

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

然后您可以在 Activity A 中引用您的 B 对象,因为当您将 ContentView 设置为 A 而不是 B 时,它被夸大了:

GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.glsurfaceview);


glSurfaceView.setRenderer();  //no longer have to pass the object

老实说,我不确定如何让它完全按照你想要的方式工作,但在你的情况下,我不确定它最终会有所作为。再说一次,我还是个新手。今天我问了一个类似的问题,我很想了解如何以另一种方式做到这一点。

【讨论】:

  • 不确定我是否理解您的 XML 修改建议。这不是和我的 XML 完全相同(我从 SurfaceViewOverlay 示例中剪切和粘贴的那个)吗?我的主要问题是点击没有传递到 Bobject 的 onTouchEvent() 方法。我相信这是因为我将它用作渲染器而不是 SurfaceViewObject。我使用的 SurfaceViewObject 是 findViewById() 返回的。
  • 仔细阅读,我不是建议更改它,而是建议更改您放置的位置。将surfaceview XML放入A的布局而不是B并膨胀A。当A包含所有视图时,无需像您那样只膨胀B的布局。
  • 我还是不明白。我不再使用 setContentView(Bobject) 了。我正在遵循 SDK 中的示例。我正在使用: setContentView(R.layout.surface_view_overlay);我很想使用 Bobject 而不是 R.layout.surface_view_overlay,但这不会显示任何按钮。
  • 如果没有您的实际代码和对您要完成的工作的更好理解,我无法提供进一步的帮助。您明确表示要向应用程序添加按钮。我不明白您为什么要尝试将这些视图添加到surfaceviewoverlay(对象B)。设计方面,我希望您通常应该将它们添加到活动 A,您的表面视图应该是其中的一部分。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2011-12-15
  • 1970-01-01
  • 2012-07-30
  • 2010-12-01
  • 2018-06-10
相关资源
最近更新 更多