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);
- 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).
I do this currently in an Android game I am making so I thought I would share my experience with it.
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?
Personally I think it is good practice to use the in-built UI widgets that Android gives you to draw your UI. There are certain situation where it wouldn't be suitable, e.g. UI controls that can be dragged around your game area or which have complicated animations or behaviour.
If you are after buttons and textViews then I recommend the approach you suggest.
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?
I use the same system to update my UI components. I don't have a need to update as often as every 16ms but I certainly don't have any issues with force closes or unresponsive UIs.
If you had many (I'm talking 10-20+) components being updated this regularly then it is possible that you could run into unresponsive UI issues.
Do you need to update the view every 16ms (around 60FPS)? You could update the underlying value to maintain correct logic but only update the View every 20-30FPS to reduce the amount run on the UI thread, if necessary.
I think UIs don't need to be updated as often as your openGL view as you don't have things wizzing around in the same way. If a textView updated 20 times per second you would still get the sense of it changing constantly.