android presentation

本文介绍如何在Android上实现双屏异显功能,利用Presentation类在辅助显示器上展示独立内容。文章详细解释了通过MediaRouter获取辅助显示并监听其变化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于双屏异显(lcd 和 hdmi 的双屏异显),android框架已经支持,但是底层接口功能还是要自己去实现,且需要底层驱动支持。

使用presentation 去画第二个display就好了。

1 MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
2 MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute();
3 if(route != null) {
4      Display presentationDisplay = route.getPresentationDisplay();
5      if (presentationDisplay != null) {
6            Presentation presentation = new MyPresentation(context, presentationDisplay);
7            presentation.show();
8      }
9 }

应用在辅助显示器上显示不同的内容程序,以有线或Wi-Fi将外接显示输出连接到用户设备上,显示独特的内容。

-------------------------------------------------------------

注意:public Presentation (Context outerContext, Display display) 这个初始化函数,这里的outerContext必须要activity,一个activity的context,虽然presentation会创建自己的context,但是它是在这个参数context之上的,而且这个activity跳转后presentation就消失了,如果说用getApplicationContext()就直接报错,也不行。

-------------------------------------------------------------

要为辅助显示屏创建独特的内容:

1. 您需要扩展Presentation类,并实现onCreate()回调方法。在onCreate()中,调用setContentView()来指定您要在辅助显示屏上显示的UI。

2. 作为Dialog类的扩展,Presentation类提供了一个区域,在其中,您的应用可以在辅助显示屏上显示不同的UI。

获取显示Presentation的辅助显示屏:

1. 可以使用DisplayManager或者MediaRouter的API。其中,使用DisplayManagerAPI可以使您获得当前连接的所有显示屏的枚举,而MediaRouter则可以使您直接访问系统为Presentation设置的默认显示输出。

2. 可以给MediaRouter.getSelectedRoute()传一个ROUTE_TYPE_LIVE_VIDEO来获得演示的默认显示器。它将返回一个MediaRouter.RouteInfo对象,描述了系统为视频演示所选择的当前路由。如果MediaRouter.RouteInfo不空,调用getPresentationDisplay()即可获取当前连接的显示屏对象:Display。

3. 然后,您可以将这个Display对象传入Presentation的构造函数。调用Presentation.show()方法,演示就会出现在辅助显示屏上了。

为在运行时即时检测新接入的显示器,需要先创建一个MediaRouter.SimpleCallback的实例。在其中,您需要实现onRoutePresentationDisplayChanged()回调方法。当新的显示器连接时,系统会调用这个回调方法。

 1 private final MediaRouter.SimpleCallback mMediaRouterCallback =
 2             new MediaRouter.SimpleCallback() {
 3         @Override
 4         public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
 5             Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info);
 6             updatePresentation();
 7         }
 8 
 9         @Override
10         public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {
11             Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info);
12             updatePresentation();
13         }
14 
15         @Override
16         public void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) {
17             Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info);
18             updatePresentation();
19         }
20     };

 

 1 private void updatePresentation() {
 2         // Get the current route and its presentation display.
 3         MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(
 4                 MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
 5         Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;
 6 
 7         // Dismiss the current presentation if the display has changed.
 8         if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {
 9             Log.i(TAG, "Dismissing presentation because the current route no longer "
10                     + "has a presentation display.");
11             mPresentation.dismiss();
12             mPresentation = null;
13         }
14 
15         // Show a new presentation if needed.
16         if (mPresentation == null && presentationDisplay != null) {
17             Log.i(TAG, "Showing presentation on display: " + presentationDisplay);
18             mPresentation = new DemoPresentation(this, presentationDisplay);
19             mPresentation.setOnDismissListener(mOnDismissListener);
20             try {
21                 mPresentation.show();
22             } catch (WindowManager.InvalidDisplayException ex) {
23                 Log.w(TAG, "Couldn‘t show presentation!  Display was removed in "
24                         + "the meantime.", ex);
25                 mPresentation = null;
26             }
27         }
28 
29         // Update the contents playing in this activity.
30         updateContents();
31     }
 1 private void updateContents() {
 2         // Show either the content in the main activity or the content in the presentation
 3         // along with some descriptive text about what is happening.
 4         if (mPresentation != null) {
 5             mInfoTextView.setText(getResources().getString(
 6                     R.string.presentation_with_media_router_now_playing_remotely,
 7                     mPresentation.getDisplay().getName()));
 8             mSurfaceView.setVisibility(View.INVISIBLE);
 9             mSurfaceView.onPause();
10             if (mPaused) {
11                 mPresentation.getSurfaceView().onPause();
12             } else {
13                 mPresentation.getSurfaceView().onResume();
14             }
15         } else {
16             mInfoTextView.setText(getResources().getString(
17                     R.string.presentation_with_media_router_now_playing_locally,
18                     getWindowManager().getDefaultDisplay().getName()));
19             mSurfaceView.setVisibility(View.VISIBLE);
20             if (mPaused) {
21                 mSurfaceView.onPause();
22             } else {
23                 mSurfaceView.onResume();
24             }
25         }
26     }

为针对辅助显示进一步优化Presentation的UI,您需要为您的应用或activity指定标签为android:presentationTheme主题。

请留意,连接到用户移动设备的屏幕往往有较大的屏幕尺寸和不同的屏幕密度。由于屏幕特征可能不同,您需要为大型显示设备提供特定优化的资源。如果您需要从Presentation中获取额外的资源,调用getContext().getResources()来获取针对这种显示的资源对象。这样,您的应用就可以根据辅助显示器的尺寸和密度提供最合适的资源了。

关于 Presentation api:http://android.toolib.net/reference/android/app/Presentation.html

<think>好的,我现在需要处理用户关于Android平台下三屏展示支持情况的查询。首先,用户提到了引文[^1]和[^2],需要确认这些引用内容是否相关。引用提到了Android 5.x的异步API示例项目,可能与多屏处理的异步任务有关。引用[^2]涉及集合视图的可重用视图类型,可能与界面布局有关。 接下来,我需要回忆Android的多屏支持功能。记得Android从4.2开始支持多屏显示,但最初可能仅限于双屏。随着版本更新,特别是Android 10(API 29)之后,官方支持了多屏显示,允许应用在多个屏幕上展示内容。不过,三屏的支持可能需要特定的配置或第三方解决方案。 然后,用户的具体问题是如何在Android上实现三屏展示。可能的实现方式包括使用Presentation类、多窗口模式或者分屏功能。需要检查这些方法是否支持超过两个屏幕。例如,Presentation类通常用于扩展显示,但可能需要硬件支持多个外部显示器。多窗口模式可能限制于设备的分屏数量,而第三方UI框架可能提供更灵活的解决方案。 还需要考虑不同Android版本的差异,以及硬件是否支持多个外部显示输出。例如,某些设备可能需要特定的适配器或驱动程序。此外,应用需要声明支持多窗口,并在清单文件中配置相应的属性。 最后,用户可能需要示例代码或配置步骤。例如,如何检测多个显示屏,如何为每个屏幕创建不同的Presentation,以及如何处理生命周期和资源管理。同时,引用中的异步API可能用于处理多屏的异步任务,而引用[^2]的集合视图可重用组件可能帮助优化多屏界面布局。 总结回答时,应分点说明支持情况、实现方式、配置要求和示例代码,确保引用相关的资料,并生成相关问题供用户进一步探索。</think>关于Android平台对三屏展示的支持情况,以下从技术实现和限制条件进行说明: 1. **系统级支持能力** - Android 10(API 29)及以上版本通过`Presentation`类原生支持多显示器输出 - 需要硬件设备支持多路视频输出(如USB-C扩展坞+HDMI组合) - 系统服务通过`DisplayManager`获取所有连接的显示屏对象: ```java DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); Display[] displays = dm.getDisplays(); ``` 2. **三屏实现方案** - **方案一**:主屏+双扩展屏 ```kotlin displays.forEach { display -> if (display.displayId != Display.DEFAULT_DISPLAY) { Presentation(ctx, display).apply { setContentView(R.layout.third_screen) }.show() } } ``` - **方案二**:使用多窗口模式(需`android:resizeableActivity="true"`清单配置) - **方案三**:第三方框架(如Scrcpy的多实例投屏方案) 3. **关键限制条件** - 设备GPU最大支持显示通道数(多数移动设备限制为3通道) - 系统级窗口管理策略(Android 13增强多窗口栈管理) - 各显示屏独立的分辨率/DPI适配要求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值