Android Presentation多屏顯示


Android Presentation

一、介紹

Presentation是一種特殊的dialog,其目的是在輔助顯示器上展示內容。 Presentation在創建時與目標Display相關聯,並根據 display 的指標配置其上下文和資源配置。當Presentation附加到的顯示器被移除時,演示文稿將自動取消。 每當活動本身暫停或恢復時,活動都應注意暫停和恢復演示文稿中正在播放的任何內容。

二、使用

1、選擇display

在顯示Presentation之前,重要的是選擇它將出現的Display 。 選擇演示顯示器有時很困難,因為可能會連接多個顯示器。 應用程序應該讓系統選擇合適的演示顯示,而不是試圖猜測哪個顯示是最好的。
選擇Display有兩種主要方法。

一、使用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();
       }
   }

二、使用DisplayManager選擇演示顯示

顯示管理器服務提供枚舉和描述連接到系統的所有顯示的功能,包括可用於演示的顯示。
顯示管理器跟蹤系統中的所有顯示。 但是,並非所有顯示器都適合顯示演示文稿。 例如,如果一個 Activity 試圖在主顯示器上顯示一個演示文稿,它可能會掩蓋它自己的內容(就像在你的 Activity 頂部打開一個對話框一樣)。

以下是如何使用DisplayManager.getDisplays(String)和DisplayManager.DISPLAY_CATEGORY_PRESENTATION類別來識別適合顯示演示文稿的顯示器。

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 = presentationDisplays[0];
       Presentation presentation = new MyPresentation(context, presentationDisplay);
       presentation.show();
   }

2、自定義演示布局

public class TestPresentation extends android.app.Presentation {
    //使用默認主題創建附加到指定顯示的新演示文稿
    public TestPresentation(Context context, Display display) {
        super(context, display);
    }
    //使用可選的指定主題創建附加到指定顯示的新演示文稿
    public TestPresentation(Context context,Display display,int theme){
        super(context,display,theme);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //自設布局
        setContentView(R.layout.presentation_test);
    }
}

三、方法

1、構造方法

//使用默認主題創建附加到指定顯示的新演示文稿
    Presentation(Context context, Display display) {
        super(context, display);
    }
//使用可選的指定主題創建附加到指定顯示的新演示文稿
    Presentation(Context context,Display display,int theme){
        super(context,display,theme);
    }

參數:

Context:顯示演示文稿的應用程序的上下文。 演示文稿將根據此上下文和有關關聯顯示的信息創建自己的上下文;

Display:演示文稿應附加到的顯示器;

theme:描述用於窗口的主題的樣式資源。如果為 0,則將使用默認演示主題。

2、公共方法

getDisplay()

獲取Display此演示文稿的Display 。

getResources()

獲取應用於擴展此演示文稿的布局的Resources 。 此資源對象已根據演示文稿出現的顯示器的指標進行配置。

onDisplayChanged()

當演示文稿附加到的Display的屬性發生更改時,由系統調用。

onDisplayRemoved()

當Display附加到的Display被移除時由系統調用。

show()

繼承自Dialog.show ,啟動對話框並將其顯示在屏幕上。

參考自谷歌官方


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM