Presentation简介

2017-10-14  本文已影响0人  OkCoco

简介

Presentation是一种在第二屏幕上进行内容显示的特殊Dialog,一个Presentation 在创建时就和一个 Display相关联,并根据Display的显示指标配置其上下文和资源配置。
值得注意的是,Presentation的Context是不同于它所包含的Activity所对应的Context的,使用Presentation自身的Context去渲染布局和加载资源是很重要的,因为他可以保证assets被以正确的尺寸和密度加载到目标Display。
当Display所依赖的窗口(或Activity)被移除时Presentation会自动Cancel,当Activity本身paused或resumed时,Activity应该保持pausing和resuming在Presentation中的播放内容。

选取Display

1.使用 MediaRouter

MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE);
 MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute();
 if (route != null) {
     Display presentationDisplay = route.getPresentationDisplay();
     if (presentationDisplay != null) {
         Presentation presentation = new MyPresentation(context, presentationDisplay);
         presentation.show();
     }
 }

2.使用 DisplayManager
The display manager keeps track of all displays in the system. However, not all displays are appropriate for showing presentations. For example, if an activity attempted to show a presentation on the main display it might obscure its own content (it's like opening a dialog on top of your activity).
译文:DisplayManager保持跟踪系统中的所有显示。 但是,并非所有显示器都适合显示Presentation。 例如,如果Activity试图在主显示器上显示Presentation,它可能会掩盖其自己的内容(这就像在您的Activity之上打开一个Dialog)。

API的使用:

DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
 Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
 if (presentationDisplays.length > 0) {
     // If there is more than one suitable presentation display, then we could consider
     // giving the user a choice.  For this example, we simply choose the first display
     // which is the one the system recommends as the preferred presentation display.
     //考虑到多屏幕的存在,返回Display[]
     Display display = presentationDisplays[0];
     Presentation presentation = new MyPresentation(context, presentationDisplay);
     presentation.show();//Presentation本身就是Dialog
 }
上一篇 下一篇

猜你喜欢

热点阅读