在我的场景中,我想在我的应用程序中镜像另一个应用程序。
我用 DisplayManager
至createVirtualDisplay
对于我的SurfaceView
.然后我设置DisplayId
像这样 VirtualDisplay
在 AcitivityOptions
当我开始 Activity 时。
通过这个过程,可以在我的surfaceview中成功启动另一个应用程序。但是有一些显示问题。根据我的测试,只有图像、背景颜色和 GLSurfaceView.Renderer
被正确渲染。 ui-component 如TextView
和 Button
未在 SurfaceView
中呈现.
从上面的测试结果来看,我猜可能和渲染的深度或者顺序有关。
示例代码如下:
SurfaceView surfaceView = findViewById(R.id.surface_view);
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
VirtualDisplay virtualDisplay = displayManager.createVirtualDisplay("MyVirtualDisplay",
surfaceView.getWidth(), surfaceView.getHeight(), 1, surfaceView.getHolder().getSurface(),
flags);
int displayId = virtualDisplay.getDisplay().getDisplayId();
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(displayId);
Intent intent = getPackageManager().getLaunchIntentForPackage(appName);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent, options.toBundle());
相比之下,如果我将相同的 Activity 镜像到
Display
而不是 VirtualDisplay
, 可以被 MediaRouter
捕获或 DisplayManager
.这样,一切都正确显示(在我的测试中,我通过“开发人员选项”->“模拟辅助显示器”模拟第二台显示器):MediaRouter mediaRouter = (MediaRouter)getSystemService(Context.MEDIA_ROUTER_SERVICE);
MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
Display presentationDisplay = route.getPresentationDisplay();
int displayId = presentationDisplay.getDisplayId();
Intent intent = getPackageManager().getLaunchIntentForPackage(appName);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityOptions options = ActivityOptions.makeBasic().setLaunchDisplayId(displayId);
startActivity(intent, options.toBundle());
是否有我错过的任何进程或我在虚拟显示模式下错误配置的任何标志?谢谢你的帮助。
附言为了镜像另一个应用程序,我已经 Root 并作为系统应用程序启动。
最佳答案
你的问题是用错误的密度Dpi调用createVirtualDisplay,它应该是更大的数字,比如480、560。
关于android - 在辅助虚拟显示器上启动时 Activity 显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59833788/