问题
I have a doubt regarding z-index on Android views. I have an activity with a GLSurfaceView and another SurfaceView for video playback. I've read on the internet that multiple surface views are not made to be placed on the same layout, but these posts are kind of old and I don't know if this is still true.
Anyway, i have problem with the order of the views. I want the VideoView to be behind the GLSurfaceView. It works when the apps starts because I call the method setZOrderOnTop(false) on the VideoView when it's constructed.
public class LBVideoView extends SurfaceView implements OnPreparedListener, OnCompletionListener, SurfaceHolder.Callback {
private Context m_context;
public LBVideoView(Context context) {
super(context);
m_context = context;
init();
}
private void init()
{
this.getHolder().addCallback(this);
this.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
setZOrderOnTop(false); //CODE TO SET VIDEO VIEW TO BACK
}
}
The problem happens when the app goes to background and is resumed later. The VideoView comes to front no matter what I do on a device that runs Android 4.1.2. On my Nexus 5 that runs Android 4.4 it never happens. Was this changed on the newest Android versions? I need to keep compatibility with older versions...
If I remove all the views and add again it works, but this doesn't help me because it destroys the surface view of the video and I don't want that to happen.
Does anyone know how to keep the surface view that is behind the GLSurfaceView behind, even after resume?
回答1:
First, #setZOrderOnTop(boolean)
, does not directly control the z-ordering of surfaces relative to each other (I don't know if it has any control over that at all.) It's documented as controlling the z-order of the surface relative to the "window" of the app.
glSurfaceView.setZOrderMediaOverlay(true)
is probably what you want. That will keep the glSurfaceView on top of the LBVideoView. AFAIK this is the only way to control the z-order of two surfaces relative to each other.
来源:https://stackoverflow.com/questions/22407039/surface-view-z-index-changing-after-onresume-onpause-android