Is there a good way to draw standard Android View
objects on top of a GLSurfaceView
and synchronize the movement between the two layers?
My layout looks like
<FrameLayout>
<RelativeLayout>
<MyCustomGLSurfaceView>
</FrameLayout>
The MyCustomGLSurfaceView
implements a camera and this camera is moved when the user touches the screen appropriately.
At the end of the onDrawFrame()
callback in the renderer I invoke a callback to tell the RelativeLayout
that the camera has moved and that it should update the location of any subviews as necessary.
To try and synchronize the two layers I am doing the following:
- Set
renderMode = RENDERMODE_WHEN_DIRTY
withinMyCustomGLSurfaceView
- Having
MyCustomGLSurfaceView
implementChoreographer.FrameCallback
and in thedoFrame(frameTimeNanos: Long)
method callingrequestRender()
- onDrawFrame invokes a callback to indicate the camera has moved and the
RelativeLayout
updates the position of it’s subviews as necessary. - Delaying the position of the camera 1 frame within the
MyCustomGLSurfaceView
since it appears that the AndroidView
objects aren't updated on the screen until the following frame whereas theMyCustomGLSurfaceView
was updated immediately.
The theory is that by listening for Choreographer
callbacks, the rendering of the MyCustomGLSurfaceView
is happening at the same time as when the application is re-drawing the standard UI elements.
This seems to work reasonably well but there are noticeable stutters within the Android View
object layer. Also positioning the elements in the Android View
layer has some odd behaviour when doing it via setting margins (views get destroyed when they're off screen and don't always reappear and sometimes stop being updated at all). I have tried positioning them using translationX / translationY
but then the synchronization between the two layers isn't as good.
Is there a good approach for achieving this synchronization?
I have thought that it might be better to use a TextureView
as opposed to a GLSurfaceView
since the TextureView
is composited into the view hierarchy rather than being a separate surface, so I thought maybe synchronization will fall out naturally from that.