Android 14 (API 級別 34) 為 子母畫面 (PiP) API 提供多工處理功能。子母畫面 Android 8.0 (API 級別 26) 開始支援這項功能,這項功能並未廣泛支援 (Android TV 支援),但 Android 之前的 Google TV 完全不支援 13.電視的多工處理功能採用子母畫面模式, 兩個獨立應用程式要在畫面上同時執行:一個以全螢幕執行 第 2 張圖片,第 2 個正在執行子母畫面模式。另有 對這兩種模式執行的應用程式各有不同的要求。
根據預設,子母畫面應用程式會重疊在全螢幕應用程式上。這是 與標準的 Android 子母畫面行為相同。
請注意,整合多工處理功能時,應用程式必須宣告 用量類型 遵守 TV 應用程式品質指南。
在子母畫面模式下執行應用程式
如果電視裝置搭載 Android 14 (API 級別 34) 以上版本,請在子母畫面模式中執行應用程式
模式。enterPictureInPictureMode()
。較早執行的電視裝置
的 Android 版本不支援子母畫面模式。
以下範例說明如何實作可輸入的按鈕邏輯 子母畫面模式:
Kotlin
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) pictureInPictureButton.visibility = if (requireActivity().packageManager.hasSystemFeature(FEATURE_PICTURE_IN_PICTURE)) { pictureInPictureButton.setOnClickListener { val aspectRatio = Rational(view.width, view.height) val params = PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .build() val result = requireActivity().enterPictureInPictureMode(params) } View.VISIBLE } else { View.GONE } }
Java
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if (requireActivity().getPackageManager().hasSystemFeature(FEATURE_PICTURE_IN_PICTURE)) { pictureInPictureButton.setVisibility(View.VISIBLE); pictureInPictureButton.setOnClickListener(v -> { Rational aspectRatio = new Rational(view.getWidth(), view.getHeight()); PictureInPictureParams params = new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setTitle("My Streaming App") .setSubtitle("My On-Demand Content") .build(); Boolean result = requireActivity().enterPictureInPictureMode(params); }); } else { pictureInPictureButton.setVisibility(View.GONE); } }
裝置必須內建系統功能,才能新增這項操作
FEATURE_PICTURE_IN_PICTURE
。此外,動作觸發時
子母畫面模式的長寬比設為符合
。
請務必新增標題和副標題,為使用者提供資訊 介紹這個子母畫面的一般用途
與在子母畫面模式下執行的應用程式並存
以全螢幕應用程式執行時,應用程式可能需要根據其他情況進行調整 應用程式。
安全無虞的 API
在某些情況下,子母畫面應用程式可能會重疊顯示 全螢幕應用程式。為緩解這種情況,應用程式可以使用清楚明瞭的 API 來識別不應重疊的重要 UI 元件。系統 盡可能避免包住這些元件 調整子母畫面視窗的位置
如要指定不應重疊檢視畫面,請在preferKeepClear
XML 版面配置,如以下範例所示:
<TextView
android:id="@+id/important_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:preferKeepClear="true"
android:text="@string/app_name"/>
您也可以使用 setPreferKeepClear()
,透過程式輔助方式執行這項操作:
Kotlin
private lateinit var binding: MyLayoutBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = MyLayoutBinding.inflate(layoutInflater) setContentView(binding.root) binding.importantText.isPreferKeepClear = true }
Java
private MyLayoutBinding binding; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = MyLayoutBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); binding.importantText.setPreferKeepClear(true); }
有時候,您可能不需要整個 View
保持清晰,
只是其中一個區段setPreferKeepClearRects()
可用於
指定不應重疊的 View
區域。不使用的 UI
原生的 View
(例如 Flutter、Jetpack Compose 和 WebView)
需要區域清晰的子區塊這個 API 可用於這類情況。
用量類型
您的應用程式必須宣告以下項目的中繼資料值屬性:
與主要類型相對應的 com.google.android.tv.pip.category
子母畫面模式的用量類型已設定的所有 <activity>
android:supportsPictureInPicture="true"
應使用
相關值。
不屬於上述任一類別的使用類型,尤其是 在電視上的子母畫面模式不允許播放媒體內容。
值 | 說明 |
---|---|
「communication 」 |
通訊用途,例如視訊或語音通話。 |
「smartHome 」 |
智慧型住宅整合服務,例如已連結的門鈴或嬰兒監視器。 |
「health 」 |
健康用途,例如健身追蹤或健康監測。 |
「ticker 」 |
即時運動賽事即時比分,或是最新消息和股票代號。 |
多個值會以直線 (|
) 分隔,例如:
<meta-data android:name="com.google.android.tv.pip.category" android:value="smartHome|health" />