Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Presentation 與自定義 Dialog 的進階用法(多螢幕異顯)

編輯:關於 Android 開發

Presentation 與自定義 Dialog 的進階用法(多螢幕異顯)

本文為原創文章,轉載請註明出處,文末附有 DEMO 供需要的朋友下載。

自 API 17 起,Android 提供了 Presentation 類別,為多螢幕異顯開發提供官方支援,當然最終能否實現仍需底層硬體與驅動配合。

基本需求:控制輔屏全螢幕顯示與主屏不同的內容,且當主屏全螢幕內容變化後,對輔屏內容的延續性(例如輔屏影片播放)不產生影響。

本文重點:

(1)Presentation 的基本用法;

(2)自定義 Dialog 的全螢幕使用;

(3)以自定義 Dialog 對話框取代 Activity 之間的切換,避免 Activity 進入 Paused 狀態導致 Presentation 不再顯示或內容中斷。例如想讓輔屏連續播放影片,若採用 Activity 切換,切換後新的 Activity 所控制的 Presentation 很難延續上一個的播放進度,且使用者體驗不佳。

Presentation 的進階用法(如硬體狀態變更事件監聽)請參考官方 API 或其他文章。

使用 Presentation 的幾個要點:

(1)生命週期:官方文件指出,當 Presentation 綁定的 Display 被移除,或 Activity 進入 paused/resuming 狀態時,都必須由我們自行控制 Presentation 的生命週期——也就是 Activity 不在主屏顯示時,由它建立的 Presentation 也將隨之消失。

(2)取得 Display:每個 Presentation 都必須指定獨立的 Display,用以呈現與主屏不同的內容。

(3)設定 ContentView:與 Activity 一樣,Presentation 也可透過 setContentView 設定要呈現的 XML 配置。

說明:為便於程式碼重構與邏輯分離,自定義的 Presentation 與 Dialog 都定義為獨立的類別;若功能簡單,也可定義為內部類別。

一、自定義 Presentation 顯示

取得 Presentation 所需的 Display 通常有 MediaRouter 與 DisplayManager 兩種方法,本文採用 DisplayManager。

(1)建立配置檔 presentation_content.xml,效果如下:

(2)建立 XiaoshubaoPresentation.java:

package xiaoshubao.presentationdemo; import android.app.Presentation; import android.content.Context; import android.os.Bundle; import android.view.Display; import android.widget.TextView; /** * 作者:xiaoshubao * 時間:2016/06/30 * 版本:V1.0 */ public class XiaoshubaoPresentation extends Presentation { private Display mdisplay; public XiaoshubaoPresentation(Context context, Display display) { super(context, display); this.mdisplay = display; } @Override protected void onStart() { super.onStart(); MessageUtils.showInfo("MyPresentation....onStart"); } @Override protected void onStop() { super.onStop(); MessageUtils.showInfo("MyPresentation....onStop"); } @Override public void show() { super.show(); MessageUtils.showInfo("MyPresentation....show"); } @Override public void hide() { super.hide(); MessageUtils.showInfo("MyPresentation....hide"); } @Override public void dismiss() { super.dismiss(); MessageUtils.showInfo("MyPresentation....dismiss"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.presentation_content); initData(); MessageUtils.showInfo("MyPresentation....savedInstanceState"); } private void initData() { TextView mPresentationId = (TextView) findViewById(R.id.presentationIdTextView); mPresentationId.setText("" + mdisplay.getDisplayId()); } }

(3)顯示 XiaoshubaoPresentation:

/** * 初始化輔屏顯示 */ private void initPresentation() { DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); if (displayManager != null) { Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); for (Display display : displays) { XiaoshubaoPresentation mPresentation = new XiaoshubaoPresentation(this, display); mPresentation.show(); mPresentationListView.add(mPresentation); } } TextView presentationCount = (TextView) findViewById(R.id.presentationCount); presentationCount.setText("輔屏數量:" + mPresentationListView.size()); }

程式碼說明:

  • 以 getSystemService(Context.DISPLAY_SERVICE) 取得 displayManager 物件,以便取得螢幕抽象物件 Display。
  • getDisplays(DISPLAY_CATEGORY_PRESENTATION) 用來挑選適合作為 Presentation 顯示的 Display(如 HDMI 或無線螢幕)。
  • 建立 Presentation 時指定呈現用的 Display。
  • 呼叫 show() 顯示 Presentation。

(4)隱藏 Presentation:

presentation.hide();

(5)釋放 Presentation 資源:

presentation.dismiss();

二、自定義 Dialog 顯示

大致思路:定義 Style 控制 Dialog 全螢幕顯示 → 定義 Dialog 的 XML 配置 → 自定義 Dialog 類別 → 響應 Dialog 控制項事件。

(1)定義 Dialog 全螢幕顯示的 Style:

<style name="XiaoshubaoDialogStyle"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style>

(2)Dialog 配置效果:

Dialog1 效果

Dialog2 效果

(3)建立 XiaoshubaoFirstDialog.java:

package xiaoshubao.presentationdemo; import android.app.Dialog; import android.content.Context; import android.view.View; import android.widget.Button; public class XiaoshubaoFirstDialog extends Dialog { private Context context; private XiaoshubaoFirstDialog dialog; public XiaoshubaoFirstDialog(Context context, int themeResId) { super(context, themeResId); this.context = context; dialog = this; } @Override public void create() { super.create(); } @Override public void show() { super.show(); initListener(); } private void initListener() { Button createSecondDialogButton = (Button) findViewById(R.id.createSecondDialogButton); createSecondDialogButton.setOnClickListener(btn_createSecondDialog); Button dismmisDialogButton = (Button) findViewById(R.id.dismmisDialogButton); dismmisDialogButton.setOnClickListener(dismmisDialogOnclick); } private View.OnClickListener btn_createSecondDialog = new View.OnClickListener() { @Override public void onClick(View v) { XiaoshubaoSecondDialog secondDialog = new XiaoshubaoSecondDialog(context, R.style.XiaoshubaoDialogStyle); secondDialog.setContentView(R.layout.dialog_second); secondDialog.show(); } }; private View.OnClickListener dismmisDialogOnclick = new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }; }

(4)建立 XiaoshubaoSecondDialog.java:

package xiaoshubao.presentationdemo; import android.app.Dialog; import android.content.Context; import android.view.View; import android.widget.Button; public class XiaoshubaoSecondDialog extends Dialog { private XiaoshubaoSecondDialog dialog; public XiaoshubaoSecondDialog(Context context, int themeResId) { super(context, themeResId); dialog = this; } @Override public void create() { super.create(); } @Override public void show() { super.show(); initListener(); } private void initListener() { Button dismmisDialogButton = (Button) findViewById(R.id.dismmisDialogButton); dismmisDialogButton.setOnClickListener(dismmisDialogOnclick); } private View.OnClickListener dismmisDialogOnclick = new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }; }

(5)設定 Dialog 控制項響應事件:

private void initListener() { Button createSecondDialogButton = (Button) findViewById(R.id.createSecondDialogButton); createSecondDialogButton.setOnClickListener(btn_createSecondDialog); Button dismmisDialogButton = (Button) findViewById(R.id.dismmisDialogButton); dismmisDialogButton.setOnClickListener(dismmisDialogOnclick); }

(6)顯示 Dialog:

public void btn_createDialogBtnClick(View v) { mainDialog = new XiaoshubaoFirstDialog(this, R.style.XiaoshubaoDialogStyle); // 設置全屏樣式 mainDialog.setContentView(R.layout.dialog_first); // 設置dialog的布局 mainDialog.show(); // 顯示dialog界面 }

(7)釋放 Dialog 資源:

dialog.dismiss();

三、綜述

專案最終呈現效果如上圖,重點是幫助初學 Android 的朋友了解多螢幕異顯,以及 Dialog 全螢幕顯示與事件響應的實作方式。DEMO 下載地址見文末。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved