画中画 (PIP) 简介
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
画中画 (PiP) 是一种特殊类型的多窗口模式,最常用于视频播放。使用该模式,用户可以通过固定到屏幕一角的小窗口观看视频,同时在应用之间进行导航或浏览主屏幕上的内容。
画中画利用 Android 7.0 中提供的多窗口模式 API 来提供固定的视频叠加窗口。如要将画中画添加到您的应用中,您需要注册 activity,根据需要将 activity 切换为画中画模式,并确保当 activity 处于画中画模式时,界面元素处于隐藏状态且视频能够继续播放。
在画中画模式下处理界面
进入画中画模式后,除非您指定界面在画中画模式和非画中画模式下的显示方式,否则应用的整个界面都会进入画中画窗口。
首先,您需要知道应用何时处于画中画模式。您可以使用 OnPictureInPictureModeChangedProvider
来实现此目的。以下代码会告知您应用是否处于 PiP 模式。
@Composable
fun rememberIsInPipMode(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val activity = LocalContext.current.findActivity()
var pipMode by remember { mutableStateOf(activity.isInPictureInPictureMode) }
DisposableEffect(activity) {
val observer = Consumer<PictureInPictureModeChangedInfo> { info ->
pipMode = info.isInPictureInPictureMode
}
activity.addOnPictureInPictureModeChangedListener(
observer
)
onDispose { activity.removeOnPictureInPictureModeChangedListener(observer) }
}
return pipMode
} else {
return false
}
}
现在,您可以使用 rememberIsInPipMode()
切换要在应用进入画中画模式时显示的界面元素:
val inPipMode = rememberIsInPipMode()
Column(modifier = modifier) {
// This text will only show up when the app is not in PiP mode
if (!inPipMode) {
Text(
text = "Picture in Picture",
)
}
VideoPlayer()
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# About picture-in-picture (PiP)\n\nPicture-in-picture (PiP) is a special type of multi-window mode mostly used for\nvideo playback. It lets the user watch a video in a small window pinned to a\ncorner of the screen while navigating between apps or browsing content on the\nmain screen.\n\nPiP leverages the multi-window APIs made available in Android 7.0 to provide the\npinned video overlay window. To add PiP to your app, you need to register your\nactivity, switch your activity to PiP mode as needed, and make sure UI elements\nare hidden and video playback continues when the activity is in PiP mode.\n\nHandle your UI in PiP mode\n--------------------------\n\nWhen you enter PiP mode, your app's entire UI enters the PiP window unless you\nspecify how your UI should look in and out of PiP mode.\n\nFirst, you need to know when your app is in PiP mode or not. You can use\n[`OnPictureInPictureModeChangedProvider`](/reference/androidx/core/app/OnPictureInPictureModeChangedProvider) to achieve this.\nThe code below tells you if your app is in PiP mode.\n\n\n```kotlin\n@Composable\nfun rememberIsInPipMode(): Boolean {\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.O) {\n val activity = LocalContext.current.findActivity()\n var pipMode by remember { mutableStateOf(activity.isInPictureInPictureMode) }\n DisposableEffect(activity) {\n val observer = Consumer\u003cPictureInPictureModeChangedInfo\u003e { info -\u003e\n pipMode = info.isInPictureInPictureMode\n }\n activity.addOnPictureInPictureModeChangedListener(\n observer\n )\n onDispose { activity.removeOnPictureInPictureModeChangedListener(observer) }\n }\n return pipMode\n } else {\n return false\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt#L119-L137\n```\n\n\u003cbr /\u003e\n\nNow, you can use `rememberIsInPipMode()` to toggle which UI elements to show\nwhen the app enters PiP mode:\n\n\n```kotlin\nval inPipMode = rememberIsInPipMode()\n\nColumn(modifier = modifier) {\n // This text will only show up when the app is not in PiP mode\n if (!inPipMode) {\n Text(\n text = \"Picture in Picture\",\n )\n }\n VideoPlayer()\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt#L152-L162\n```\n\n\u003cbr /\u003e"]]