Android Presentation實現(xiàn)雙屏異顯
一、概述
現(xiàn)在越來越多的Android設備有多個屏幕,雙屏異顯應用場景最多的應該就是類似于收銀平臺那種設備,在主屏上店員能夠對點商品進行選擇錄入,副屏則是展示給我們的賬單詳情,但是它只通過了一個軟件系統(tǒng)就實現(xiàn)了雙屏異顯這個功能,而Presentation正是這其中的關鍵。
二、Presentation分析
1.簡述:首先從它的繼承關系上來看Presentation是繼承自Dialog的,就是說它其實就是一種特殊的Dialog用于在第二個屏幕上顯示內(nèi)容的,它在創(chuàng)建時會和它的目標展示屏幕相關聯(lián),包括它的context和一些配置參數(shù)等。
2.Context:然而這里提到的context和它的容器所處的context會有不同,它會用自身的context去加載presentation的布局和相關資源以此來確保在目標屏幕上能夠展示正確的大小和獲取合適的屏幕密度。
3.自動移除:雖說presentation和它相關聯(lián)的Activity的context不同,但是他們也不是完全分離的關系,當和presentation相關聯(lián)的屏幕被移除后,presentation也會自動的被移除,所以當Activity處于pause和resume的狀態(tài)時Presentation也需要特別注意當前顯示的內(nèi)容的狀態(tài)。
4.屏幕選擇:因為有時候我們的Android設備有多個屏幕,所以選擇合適的屏幕去展示就顯得非常重要了,所以在我們顯示Presentation的時候需要去讓我們的系統(tǒng)去選擇合適的屏幕來進行展示,以下是兩種方式去選擇一個合適的屏幕。
這是我們選擇展示presentation最簡單的一種方式,media router是一種系統(tǒng)層級的服務,它能夠追蹤到系統(tǒng)當中所有可用的音頻和視屏route,當有路徑被選中或取消選中,還有當適合用presentation進行顯示的時候的route改變的時候它會發(fā)送一個通知,然后應用本身會監(jiān)控這個通知自動的去選擇presentation的展示或者隱藏,這里的推薦使用的presentation display其實只是media router推薦的,如果我們的應用需要在第二個屏幕上進行顯示就使用,如果不用的話就用本地來展示內(nèi)容。
- 利用media router去選擇presentation的顯示屏幕
- 利用display manager去選擇persentation的顯示屏幕
DisplayManager能夠監(jiān)控到我們系統(tǒng)當中的所有連接上的顯示設備,然而不是所有的設備都適用于作為副屏來進行內(nèi)容展示的,比如當一個Activity想顯示一個presentation在主屏幕上,其實效果就會相當于在主Activity當中顯示了一個特殊的Dialog,所以當我們選擇這種方式去選用Presentation去顯示的時候就必須給它綁定一個可用的display。
三、源碼分析
首先來看它的構造函數(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);
}
在它的形參中第一個Context參數(shù)非常重要,這是應用正在展示presentation的一個context,它是Presentation自己創(chuàng)建的它自己的一個context,基于這個context才能正確的在它所關聯(lián)的屏幕上展示合適的信息。然后代碼里面設置了這個window的相關屬性。
接著我們看看它自身的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);
//這里是對它的主題的判斷,為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給我們創(chuàng)建好的Context
return new ContextThemeWrapper(displayContext, theme) {
//在這個方法中又返回的是Object對象
@Override
public Object getSystemService(String name) {
if (WINDOW_SERVICE.equals(name)) {
return displayWindowManager;
//如果和這個傳入的name相同的話返回的就是上面windowManager創(chuàng)建出來的WindowManager的一個具體實現(xiàn)
}
//否則就返回的是Context這個抽象類中的一種服務類型
return super.getSystemService(name);
}
};
}
接著我們繼續(xù)看一下Presentation對屏幕增加、移除和改變的監(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();
}
}
};
這里我們看到它對add并沒有進行處理,所以我們進一步去看一下onDisplayRemoved中的關鍵handlerDisplayRemoved和onDisplayChanged中的核心handlerDisplayChanged的實現(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();
}
}
在這個方法中,我們遺憾的發(fā)現(xiàn)Google程序員并沒有對當前屏幕配置發(fā)生改變后做特殊的處理,所以當我們的屏幕尺寸等信息改變時,我們的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中我們也能發(fā)現(xiàn),因為它暫時還沒有對display change進行監(jiān)聽,而這里又是有可能會改變的,所以在這種情況下它是打印了一個log去通知一下。
handlerDisplayRemoved這個方法的話是系統(tǒng)自動去發(fā)送一個消息,然后調(diào)用后把presentation自動取消掉。
四、總結
在基本分析了Presentation之后,主要要注意一下它的Context以及和Activity之間綁定的關系,其實從簡單來看,它就是一個Dialog,不過是可以顯示在多個屏幕上。當然上述分析深度尚淺,更深入的理解和一些問題要在后續(xù)工作中多使用再繼續(xù)觀察。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導航的方法
ImageSwitcher是Android中控制圖片展示效果的一個控件。本文給大家介紹Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導航的方法,需要的朋友參考下吧2016-12-12

