android - 在 GLSurfaceView 之上绘制 Android UI

标签 android glsurfaceview

对于我的游戏,我正在考虑使用 RelativeLayout 在我的 GLSurfaceView 之上绘制 UI 元素(用于显示耗时的 TextView,用于暂停/重新启动游戏的按钮)...

到目前为止,我都是自己直接将所有 UI 元素绘制到 surfaceView,但考虑到 Android UI 提供的广泛选项(如更改字体和颜色的字体),我决定使用它。

问题:

  • GLSurfaceView 之上绘制 UI 元素是一种好习惯,还是我自己直接将所有 UI 元素绘制到 GLSurfaceView 更好?
  • 我将使用 runOnUiThread() 方法定期(每 16 毫秒)更新 UI 元素(比如 TextView)的内容...这不好吗?
  • 我的游戏是否容易受到强制关闭的影响?

这是我用来设置 RelativeLayout 的测试 java 代码... glViewGLSurfaceView 实例。

    rl = new RelativeLayout(this);
    rl.addView(glView);        
    tv = new TextView(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_TOP);
    tv.setLayoutParams(lp);
    tv.setText("FPS: 0");
    tv.setBackgroundColor(0x4060ff70);
    rl.addView(tv);

    setContentView(rl);

最佳答案

  • Is it good practice to draw UI elements on top of GLSurfaceView or is it better to draw all the UI stuff myself directly to GLSurfaceView?

如果您需要 android 布局系统提供的灵 active ,那么在 GLSurfaceView 之上绘制 android 控件似乎是个好主意。警告尽管这些控件的绘制速度可能比 glSurfaceView 慢得多,因此请确保 UI 不需要太多的图形能力来呈现并且不会更新得太快。

自己渲染所有内容是一种选择,但这样您就失去了可以从 Android UI 系统获得的所有功能。

  • I will be updating the content of the UI elements (Say TextView) regularly (for each 16 ms) by using runOnUiThread() method... Is this bad?

我觉得不错。

  • Will my game be susceptible for Force Closes?

这与其他问题没有任何关系。如果您遵循正确的用法并且不会导致抛出异常,那么您应该没问题。如果您确实导致了异常,那么您的应用将被强制关闭(除非您正确处理了异常)。

关于android - 在 GLSurfaceView 之上绘制 Android UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254320/

相关文章:

android - SurfaceView 还是 GLSurfaceview?

android - Android:初学者使用Opencv库(转换为opencv格式)

android - 如何初始化依赖于 View 属性的东西

android - 谷歌地图有商业许可证吗?

android - 如何在填充包含在 TextInputLayout 中的 EditText 字段时禁用 float 标签动画

android - 前置摄像头2 API GLSurfaceview

Android GLSurfaceView 2.x SDK 保留上下文。屏幕关闭时销毁

android - 如何直接向 FrameLayout 添加元素 (android :id/content?)

android - ListView 占用了 LinearLayout 中的所有空间

android - 找不到符号变量editText