0

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

  • Start stream
  • In undetermined period of stream there may come specific data which indicates that my streaming capabilities have changed
  • Update VirtualDisplay's parameters without recreation, so that state loss is avoided

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

4

1 回答 1

0

回答我自己的问题

调整大小的方法VirtualDisplay工作得很好,虽然这是我对如何实现非常具体的行为的误解,当只有底层布局改变它的大小时,尽管元素将它们的属性保持在较小的窗口上

因此,如果您想获得某种“标量”调整大小(就像一切都变大或变小),您应该调用resize

但是,当您的项目需要某种调整大小并使控件
更大时,尽管屏幕变小
了,您应该检查Presentation链接的具体类的扩展,VirtualDisplay并手动更新布局而不VirtualDisplay调整大小

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();
            }
        }
    });
}

uiHandler即使在背景中也可以简单地获得哪里

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

我希望这对某人有用!

于 2020-04-30T09:24:06.707 回答