androidresizevideo-streamingandroid-mediaprojectionandroid-virtualdisplay

Android VirtualDisplay resize "on flight"


Recently in project I faced challenge of resizing VirtualDisplay "on flight". So the use case is :

I've found in documentation for VirtualDisplay resize method, though it seems to have no effect on new parameters incoming. For implementation I am using

  virtualDisplay = mDisplayManager.createVirtualDisplay("DispName",
                        getResolution().getResolutionWidth(), getResolution().getResolutionHeight(),
                        getDisplayDensity(), inputSurface, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION);

where inputSurface is created by mediaEncoder.createInputSurface() and cofigured properly by this moment. So, the question is, how can I resize VirtualDisplay? I also didn't find any examples how to do it in official sources, would appreciate any help!

UPDATE Just forgot to mention, I've put Listener for VirtualDisplays and onChange method is triggered, though check if actual metrics were changed shows negative results


Solution

  • Answering to my own question

    The resize method of VirtualDisplay works pretty fine, though it was from my side misunderstanding of how to achieve very specific behaviour, when only underlying layout changes it's size, though elements are keeping their properties on smaller window

    So, in case if you want to get some kind of "scalar" resize (like everything comes bigger or smaller) you should call resize

    But, whenewer your project demands some kind of resizing and making your controls
    bigger, though screen comes smaller
    you should check your extention of your concrete Presentation class linked with VirtualDisplay and just update your layout manually without having VirtualDisplay resized

    public void resizeView(final int newWidth, final int newHeight) {
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    Constructor<? extends ViewGroup.LayoutParams> ctor = 
                            mainView.getLayoutParams().getClass().getDeclaredConstructor(int.class, int.class);
                    mainView.setLayoutParams(ctor.newInstance(newWidth, newHeight));
                    mainView.requestLayout();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    

    where uiHandler could be simply obtained even in background with

    Handler uiHandler = new Handler(Looper.getMainLooper());
    

    I hope this would be useful for somebody!