Android 14 (API 級別 34) 為 子母畫面 (PiP) API 提供多工處理功能。子母畫面 Android 8.0 (API 級別 26) 開始支援這項功能,這項功能並未廣泛支援 (Android TV 支援),但 Android 之前的 Google TV 完全不支援 13.電視的多工處理功能採用子母畫面模式, 兩個獨立應用程式要在畫面上同時執行:一個以全螢幕執行 第 2 張圖片,第 2 個正在執行子母畫面模式。在上述任一模式下執行的應用程式,須符合不同的規定。
預設行為是子母畫面應用程式會疊加全螢幕應用程式。這與標準 Android 子母畫面行為非常相似。
請注意,在整合多工處理功能時,應用程式必須遵循 TV 應用程式品質指南,宣告其使用類型。
在 PiP 模式下執行應用程式
如果電視裝置搭載 Android 14 (API 級別 34) 以上版本,請在子母畫面模式中執行應用程式
模式。enterPictureInPictureMode()
。搭載舊版 Android 的電視裝置不支援 PiP 模式。
以下範例說明如何實作按鈕的邏輯,以便進入 PiP 模式:
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
。此外,動作觸發時
子母畫面模式的長寬比設為符合
。
請務必新增標題和副標題,為使用者提供資訊 介紹這個子母畫面的一般用途
與在 PiP 模式下執行的應用程式共存
以全螢幕應用程式執行時,應用程式可能需要根據其他情況進行調整 應用程式。
Keep-clear API
在某些情況下,PiP 應用程式可能會在全螢幕應用程式中疊加重要 UI 元件。為避免這種情況發生,應用程式可使用 keep-clear 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" />