Drawing Android UI on top of GLSurfaceView

BLOB picture BLOB · Jan 10, 2013 · Viewed 6.9k times · Source

For my game, I am thinking of drawing UI elements (TextView for displaying time elapsed, Buttons for pausing/restarting game) on top of my GLSurfaceView using RelativeLayout...

Till now, I was drawing all the UI elements myself directly to surfaceView but considering the wide options available from Android UI (like changing typeface of font and color), I have decided to use it.

Questions:

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

Here is the test java code which i am using to set up the RelativeLayout... glView is the GLSurfaceView instance.

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

Answer

Tim picture Tim · Jan 10, 2013
  • 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?

It seems a fine idea to draw android controls on top of a GLSurfaceView if you desire the flexibility that the android layout system provides. Warning though that these controls may be drawn much more slowly than your glSurfaceView, so make sure the UI doesn't need too much graphical power to render and doesn't update too quickly.

Rendering everything yourself is an option, but then you lose all of the features you can get from the android UI system.

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

Sounds fine to me.

  • Will my game be susceptible for Force Closes?

This doesn't have anything to do with the other questions. If you follow proper usage and don't cause exceptions to be thrown, then you should be fine. If you do cause exceptions then your app will be forced closed (unless you properly handle the exceptions).