Skip to content Skip to sidebar Skip to footer

How To Properly Use Setzordermediaoverlay On Android?

Like many others, I am trying to draw 3D objects (using GLSurfaceView) on camera preview (using SurfaceView), along with some buttons placed on top. I actually got a prototype work

Solution 1:

I have struggled with the same issue for some time, but believe the code below now works. (It continues to work after pausing/ locking regardless of whether the camera is on or off).

Firstly, I haven't got any views etc defined in the layout file - they're created in code. The following is called from the main onCreate() method - Note the setZOrderMediaOverlay() on the augScreen.

I have done nothing special within onPause() or onResume() apart from passing onPause() and onResume() through to the augScreen.

// 1 - Camera Preview
        camScreen = newCameraPreview(this);
        setContentView(camScreen, newLayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    // 2 - 3D object display
        augScreen = newAR_SurfaceView(this, getViewRange());
        addContentView(augScreen, newLayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        augScreen.setZOrderMediaOverlay(true);

    // 3 - Permanent overlayRelativeLayoutoverlayScreen=newOverlayView(this);
    addContentView(overlayScreen, newLayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    overlayScreen.setVisibility(View.VISIBLE);

    // 4 - UI buttons (toggleable)
        uiScreen = newUserInterfaceView(this);
        addContentView(uiScreen, newLayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

Solution 2:

Awila_Tech's answer helped me as well. I used a GLSurfaceView and neatly called its onPause and onResume in the Activities's onPause and onResume, but 1 out of 5 times my screen stayed black while the actual onDrawFrame was being called.

To summarize:

publicclassOpenGLAppextendsActivityimplementsGLSurfaceView.Renderer {
    privateGLSurfaceView mGLSurfaceView;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGLSurfaceView = newGLSurfaceView(this);
        mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 8);
        mGLSurfaceView.setRenderer(this);
        mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        mGLSurfaceView.setKeepScreenOn(true);
        mGLSurfaceView.setDrawingCacheEnabled(true);
        mGLSurfaceView.setZOrderOnTop(true);

        setContentView(mGLSurfaceView);
        //...
    }

    @OverrideprotectedvoidonPause() {
        //...
        mGLSurfaceView.onPause();
        super.onPause();
    }

    @OverrideprotectedvoidonResume() {
        mGLSurfaceView.onResume();
        //...super.onResume();
    }
}

Post a Comment for "How To Properly Use Setzordermediaoverlay On Android?"