在我的方案中,我想在我的应用中镜像另一个应用。
我用DisplayManager
为我的SurfaceView
createVirtualDisplay
。然后,当我开始活动时,我将DisplayId
设置为AcitivityOptions
VirtualDisplay
。
通过此过程,可以在我的 surfaceview 中成功启动另一个应用。但是存在一些显示问题。根据我的测试,只有图像,背景颜色和GLSurfaceView.Renderer
才能正确呈现。TextView
和Button
等 ui 组件不会以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());
相比之下,如果我将相同的活动镜像到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());
在虚拟显示模式下,我是否错过了任何进程或错误配置了任何标志?感谢您的任何帮助。
附言为了镜像另一个应用程序,我已经植根并作为系统应用程序启动。
您的问题是使用错误的密度Dpi调用createVirtualDisplay,它应该是更大的数字,例如480,560。