android - 在辅助虚拟显示器上启动时 Activity 显示不正确

标签 android api user-interface display android-virtualdisplay

在我的场景中,我想在我的应用程序中镜像另一个应用程序。

我用 DisplayManagercreateVirtualDisplay对于我的SurfaceView .然后我设置DisplayId像这样 VirtualDisplayAcitivityOptions当我开始 Activity 时。

通过这个过程,可以在我的surfaceview中成功启动另一个应用程序。但是有一些显示问题。根据我的测试,只有图像、背景颜色和 GLSurfaceView.Renderer被正确渲染。 ui-component 如TextViewButton未在 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/

相关文章:

java - 在 android 中实现 NavigationView

java - Rest、Rest Api、Web Service、RestFul Api 之间的区别

java - 如何正确对齐标签?

c - 将用 C 编写的现有 Windows GUI 应用程序移植到 Linux 有哪些选择?

c# - Windows 窗体/WPF 太大,我该如何拆分?

android - 如何将现有的 Android 项目导入 Eclipse?

android - 将与菜单相关的事件称为 "callback function"是否有意义?

reactjs - 类型错误 : "NetworkError when attempting to fetch resource."

javascript - 在reactjs中创建2个对象之间的关系(OnetToMany)

java - 更新 ListView 中的按钮