欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Presentation實(shí)現(xiàn)雙屏異顯

 更新時(shí)間:2020年01月21日 14:54:52   作者:Leon_Jcy  
這篇文章主要為大家詳細(xì)介紹了Android Presentation實(shí)現(xiàn)雙屏異顯,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、概述

現(xiàn)在越來越多的Android設(shè)備有多個(gè)屏幕,雙屏異顯應(yīng)用場(chǎng)景最多的應(yīng)該就是類似于收銀平臺(tái)那種設(shè)備,在主屏上店員能夠?qū)c(diǎn)商品進(jìn)行選擇錄入,副屏則是展示給我們的賬單詳情,但是它只通過了一個(gè)軟件系統(tǒng)就實(shí)現(xiàn)了雙屏異顯這個(gè)功能,而Presentation正是這其中的關(guān)鍵。

二、Presentation分析

1.簡(jiǎn)述:首先從它的繼承關(guān)系上來看Presentation是繼承自Dialog的,就是說它其實(shí)就是一種特殊的Dialog用于在第二個(gè)屏幕上顯示內(nèi)容的,它在創(chuàng)建時(shí)會(huì)和它的目標(biāo)展示屏幕相關(guān)聯(lián),包括它的context和一些配置參數(shù)等。

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

3.自動(dòng)移除:雖說presentation和它相關(guān)聯(lián)的Activity的context不同,但是他們也不是完全分離的關(guān)系,當(dāng)和presentation相關(guān)聯(lián)的屏幕被移除后,presentation也會(huì)自動(dòng)的被移除,所以當(dāng)Activity處于pause和resume的狀態(tài)時(shí)Presentation也需要特別注意當(dāng)前顯示的內(nèi)容的狀態(tài)。

4.屏幕選擇:因?yàn)橛袝r(shí)候我們的Android設(shè)備有多個(gè)屏幕,所以選擇合適的屏幕去展示就顯得非常重要了,所以在我們顯示Presentation的時(shí)候需要去讓我們的系統(tǒng)去選擇合適的屏幕來進(jìn)行展示,以下是兩種方式去選擇一個(gè)合適的屏幕。

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

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

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

三、源碼分析

首先來看它的構(gòu)造函數(shù)

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);
 }

在它的形參中第一個(gè)Context參數(shù)非常重要,這是應(yīng)用正在展示presentation的一個(gè)context,它是Presentation自己創(chuàng)建的它自己的一個(gè)context,基于這個(gè)context才能正確的在它所關(guān)聯(lián)的屏幕上展示合適的信息。然后代碼里面設(shè)置了這個(gè)window的相關(guān)屬性。

接著我們看看它自身的Context的創(chuàng)建

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);
 
 //這里是對(duì)它的主題的判斷,為0即為默認(rèn)主題
 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);
 
 //因?yàn)镃ontextThemeWrapper的父類是我們的Context
 //所以這里最終返回的就是Presentation給我們創(chuàng)建好的Context
 return new ContextThemeWrapper(displayContext, theme) {
 
 //在這個(gè)方法中又返回的是Object對(duì)象
 @Override
 public Object getSystemService(String name) {
 if (WINDOW_SERVICE.equals(name)) {
  return displayWindowManager;
  //如果和這個(gè)傳入的name相同的話返回的就是上面windowManager創(chuàng)建出來的WindowManager的一個(gè)具體實(shí)現(xiàn)
 }
 //否則就返回的是Context這個(gè)抽象類中的一種服務(wù)類型
 return super.getSystemService(name);
 }
 };
 }

接著我們繼續(xù)看一下Presentation對(duì)屏幕增加、移除和改變的監(jiān)聽

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();
 }
 }
 };

這里我們看到它對(duì)add并沒有進(jìn)行處理,所以我們進(jìn)一步去看一下onDisplayRemoved中的關(guān)鍵handlerDisplayRemoved和onDisplayChanged中的核心handlerDisplayChanged的實(shí)現(xiàn)

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();
 }
 }

在這個(gè)方法中,我們遺憾的發(fā)現(xiàn)Google程序員并沒有對(duì)當(dāng)前屏幕配置發(fā)生改變后做特殊的處理,所以當(dāng)我們的屏幕尺寸等信息改變時(shí),我們的presentation必須重新Create去重新獲取context,再重新進(jìn)行顯示。

@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);
 }
 }

同時(shí)在onStart中我們也能發(fā)現(xiàn),因?yàn)樗鼤簳r(shí)還沒有對(duì)display change進(jìn)行監(jiān)聽,而這里又是有可能會(huì)改變的,所以在這種情況下它是打印了一個(gè)log去通知一下。 

handlerDisplayRemoved這個(gè)方法的話是系統(tǒng)自動(dòng)去發(fā)送一個(gè)消息,然后調(diào)用后把presentation自動(dòng)取消掉。

四、總結(jié)

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論