首先,在你的 AndroidManifest.xml
文件中声明支持画中画模式:
<activity
android:name=".YourActivity"
android:supportsPictureInPicture="true"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation">
</activity>
在你的 Activity
中,当需要切换到画中画模式时,可以调用 enterPictureInPictureMode()
方法:
fun enterPipMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val aspectRatio = Rational(16, 9)
val pipParams = PictureInPictureParams.Builder()
.setAspectRatio(aspectRatio)
.build()
enterPictureInPictureMode(pipParams)
}
}
你可以覆盖 onPictureInPictureModeChanged
方法来处理进入和退出画中画模式的回调:
override fun onPictureInPictureModeChanged(
isInPictureInPictureMode: Boolean,
newConfig: Configuration?
) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
if (isInPictureInPictureMode) {
// 隐藏不必要的 UI 元素
} else {
// 恢复 UI 元素
}
}
你可以为画中画模式添加自定义操作,例如播放、暂停等:
fun createPipActions() {
val intent = PendingIntent.getActivity(
this, 0, Intent(this, YourActivity::class.java), 0
)
val icon = Icon.createWithResource(this, R.drawable.ic_play)
val action = RemoteAction(icon, "Play", "Play", intent)
val pipParams = PictureInPictureParams.Builder()
.setActions(listOf(action))
.build()
setPictureInPictureParams(pipParams)
}
确保你的 Activity
能够处理配置变更,以避免在画中画模式下重建:
<activity
android:name=".YourActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation">
</activity>
以下是一个完整的示例,展示如何在 Activity
中实现画中画模式:
class YourActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_your)
// 进入画中画模式的按钮点击事件
findViewById<Button>(R.id.enter_pip_button).setOnClickListener {
enterPipMode()
}
}
private fun enterPipMode() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val aspectRatio = Rational(16, 9)
val pipParams = PictureInPictureParams.Builder()
.setAspectRatio(aspectRatio)
.build()
enterPictureInPictureMode(pipParams)
}
}
override fun onPictureInPictureModeChanged(
isInPictureInPictureMode: Boolean,
newConfig: Configuration?
) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
if (isInPictureInPictureMode) {
// 隐藏不必要的 UI 元素
} else {
// 恢复 UI 元素
}
}
}