Android 14 (API 級別 34) 針對子母畫面 (PiP) API 推出一些強化功能,可支援多工處理。雖然 Android 8.0 (API 級別 26) 推出子母畫面支援,但 Android TV 並未廣泛支援,Android 13 之前的 Google TV 則完全不支援。電視的多工處理功能會使用子母畫面模式,讓兩個獨立的應用程式在螢幕上並存:一個以全螢幕模式執行,另一個則以子母畫面模式執行。在任一模式下執行的應用程式,都有不同的要求。
預設行為是子母畫面應用程式會疊加在全螢幕應用程式上。這與標準 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 時,系統才會新增這項動作。此外,觸發動作時,子母畫面模式的顯示比例會設為與播放中的影片相同。
請務必新增標題和副標題,向使用者提供這個 PIP 的一般用途資訊。
與在子母畫面模式下執行的應用程式並存
當應用程式以全螢幕模式執行時,可能需要配合其他以子母畫面模式執行的應用程式。
Keep-clear API
在某些情況下,子母畫面應用程式可能會疊加在全螢幕應用程式的重要 UI 元件上。為避免這種情況,應用程式可以使用 Keep-Clear API,找出不應疊加的重要 UI 元件。系統會嘗試回應要求,重新調整子母畫面視窗的位置,避免遮蓋這些元件。
如要指定不應疊加檢視畫面,請在 XML 版面配置中使用 preferKeepClear,如以下範例所示:
<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 的中繼資料值屬性,該屬性會對應至子母畫面模式的主要使用類型。任何已設定 android:supportsPictureInPicture="true" 的 <activity> 都應使用下表中的相關值宣告這個屬性。
不屬於上述任何類別的使用類型 (尤其是播放媒體內容),都不得在電視上使用子母畫面模式。
| 值 | 說明 |
|---|---|
「communication」 |
通訊用途,例如視訊或語音通話。 |
「smartHome」 |
智慧型住宅整合功能,例如連結的門鈴或嬰兒監視器。 |
「health」 |
Health 用途,例如運動智慧手環追蹤或健康監測。 |
「ticker」 |
資訊看板用途,例如體育賽事即時比分、新聞和股票資訊看板。 |
多個值之間以直線號 (|) 分隔。例如:
<meta-data android:name="com.google.android.tv.pip.category" android:value="smartHome|health" />