溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android Presentation如何實現雙屏異顯

發布時間:2021-05-27 14:33:51 來源:億速云 閱讀:315 作者:小新 欄目:移動開發

這篇文章主要介紹Android Presentation如何實現雙屏異顯,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、概述

現在越來越多的Android設備有多個屏幕,雙屏異顯應用場景最多的應該就是類似于收銀平臺那種設備,在主屏上店員能夠對點商品進行選擇錄入,副屏則是展示給我們的賬單詳情,但是它只通過了一個軟件系統就實現了雙屏異顯這個功能,而Presentation正是這其中的關鍵。

二、Presentation分析

1.簡述:首先從它的繼承關系上來看Presentation是繼承自Dialog的,就是說它其實就是一種特殊的Dialog用于在第二個屏幕上顯示內容的,它在創建時會和它的目標展示屏幕相關聯,包括它的context和一些配置參數等。

2.Context:然而這里提到的context和它的容器所處的context會有不同,它會用自身的context去加載presentation的布局和相關資源以此來確保在目標屏幕上能夠展示正確的大小和獲取合適的屏幕密度。

3.自動移除:雖說presentation和它相關聯的Activity的context不同,但是他們也不是完全分離的關系,當和presentation相關聯的屏幕被移除后,presentation也會自動的被移除,所以當Activity處于pause和resume的狀態時Presentation也需要特別注意當前顯示的內容的狀態。

4.屏幕選擇:因為有時候我們的Android設備有多個屏幕,所以選擇合適的屏幕去展示就顯得非常重要了,所以在我們顯示Presentation的時候需要去讓我們的系統去選擇合適的屏幕來進行展示,以下是兩種方式去選擇一個合適的屏幕。

這是我們選擇展示presentation最簡單的一種方式,media router是一種系統層級的服務,它能夠追蹤到系統當中所有可用的音頻和視屏route,當有路徑被選中或取消選中,還有當適合用presentation進行顯示的時候的route改變的時候它會發送一個通知,然后應用本身會監控這個通知自動的去選擇presentation的展示或者隱藏,這里的推薦使用的presentation display其實只是media router推薦的,如果我們的應用需要在第二個屏幕上進行顯示就使用,如果不用的話就用本地來展示內容。  

  • 利用media router去選擇presentation的顯示屏幕

  • 利用display manager去選擇persentation的顯示屏幕

DisplayManager能夠監控到我們系統當中的所有連接上的顯示設備,然而不是所有的設備都適用于作為副屏來進行內容展示的,比如當一個Activity想顯示一個presentation在主屏幕上,其實效果就會相當于在主Activity當中顯示了一個特殊的Dialog,所以當我們選擇這種方式去選用Presentation去顯示的時候就必須給它綁定一個可用的display。

三、源碼分析

首先來看它的構造函數

public Presentation(Context outerContext, Display display, int theme) {
 super(createPresentationContext(outerContext, display, theme), theme, false);
 
 mDisplay = display;
 mDisplayManager = (DisplayManager)getContext().getSystemService(DISPLAY_SERVICE);
 
 final Window w = getWindow();
 final WindowManager.LayoutParams attr = w.getAttributes();
 attr.token = mToken;
 w.setAttributes(attr);
 w.setGravity(Gravity.FILL);
 w.setType(TYPE_PRESENTATION);
 setCanceledOnTouchOutside(false);
 }

在它的形參中第一個Context參數非常重要,這是應用正在展示presentation的一個context,它是Presentation自己創建的它自己的一個context,基于這個context才能正確的在它所關聯的屏幕上展示合適的信息。然后代碼里面設置了這個window的相關屬性。

接著我們看看它自身的Context的創建

private static Context createPresentationContext(
 Context outerContext, Display display, int theme) {
 //首先判斷傳入的context和display是否為空,為空則拋出異常
 if (outerContext == null) {
 throw new IllegalArgumentException("outerContext must not be null");
 }
 if (display == null) {
 throw new IllegalArgumentException("display must not be null");
 }
 
 Context displayContext = outerContext.createDisplayContext(display);
 
 //這里是對它的主題的判斷,為0即為默認主題
 if (theme == 0) {
 TypedValue outValue = new TypedValue();
 displayContext.getTheme().resolveAttribute(
  com.android.internal.R.attr.presentationTheme, outValue, true);
 theme = outValue.resourceId;
 }
 
 // Derive the display's window manager from the outer window manager.
 // We do this because the outer window manager have some extra information
 // such as the parent window, which is important if the presentation uses
 // an application window type.
 final WindowManagerImpl outerWindowManager =
 (WindowManagerImpl)outerContext.getSystemService(WINDOW_SERVICE);
 final WindowManagerImpl displayWindowManager =
 outerWindowManager.createPresentationWindowManager(displayContext);
 
 //因為ContextThemeWrapper的父類是我們的Context
 //所以這里最終返回的就是Presentation給我們創建好的Context
 return new ContextThemeWrapper(displayContext, theme) {
 
 //在這個方法中又返回的是Object對象
 @Override
 public Object getSystemService(String name) {
 if (WINDOW_SERVICE.equals(name)) {
  return displayWindowManager;
  //如果和這個傳入的name相同的話返回的就是上面windowManager創建出來的WindowManager的一個具體實現
 }
 //否則就返回的是Context這個抽象類中的一種服務類型
 return super.getSystemService(name);
 }
 };
 }

接著我們繼續看一下Presentation對屏幕增加、移除和改變的監聽

private final DisplayListener mDisplayListener = new DisplayListener() {
 @Override
 public void onDisplayAdded(int displayId) {
 }
 
 @Override
 public void onDisplayRemoved(int displayId) {
 if (displayId == mDisplay.getDisplayId()) {
 handleDisplayRemoved();
 }
 }
 
 @Override
 public void onDisplayChanged(int displayId) {
 if (displayId == mDisplay.getDisplayId()) {
 handleDisplayChanged();
 }
 }
 };

這里我們看到它對add并沒有進行處理,所以我們進一步去看一下onDisplayRemoved中的關鍵handlerDisplayRemoved和onDisplayChanged中的核心handlerDisplayChanged的實現

handlerDisplayChanged:

private void handleDisplayChanged() {
 onDisplayChanged();
 
 // We currently do not support configuration changes for presentations
 // (although we could add that feature with a bit more work).
 // If the display metrics have changed in any way then the current configuration
 // is invalid and the application must recreate the presentation to get
 // a new context.
 if (!isConfigurationStillValid()) {
 Log.i(TAG, "Presentation is being dismissed because the "
  + "display metrics have changed since it was created.");
 cancel();
 }
 }

在這個方法中,我們遺憾的發現Google程序員并沒有對當前屏幕配置發生改變后做特殊的處理,所以當我們的屏幕尺寸等信息改變時,我們的presentation必須重新Create去重新獲取context,再重新進行顯示。

@Override
 protected void onStart() {
 super.onStart();
 mDisplayManager.registerDisplayListener(mDisplayListener, mHandler);
 
 // Since we were not watching for display changes until just now, there is a
 // chance that the display metrics have changed. If so, we will need to
 // dismiss the presentation immediately. This case is expected
 // to be rare but surprising, so we'll write a log message about it.
 if (!isConfigurationStillValid()) {
 Log.i(TAG, "Presentation is being dismissed because the "
  + "display metrics have changed since it was created.");
 mHandler.sendEmptyMessage(MSG_CANCEL);
 }
 }

同時在onStart中我們也能發現,因為它暫時還沒有對display change進行監聽,而這里又是有可能會改變的,所以在這種情況下它是打印了一個log去通知一下。 

handlerDisplayRemoved這個方法的話是系統自動去發送一個消息,然后調用后把presentation自動取消掉。

四、總結

在基本分析了Presentation之后,主要要注意一下它的Context以及和Activity之間綁定的關系,其實從簡單來看,它就是一個Dialog,不過是可以顯示在多個屏幕上。當然上述分析深度尚淺,更深入的理解和一些問題要在后續工作中多使用再繼續觀察。

Android是什么

Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。

以上是“Android Presentation如何實現雙屏異顯”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女