Android画中画窗口开启方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android画中画窗口开启方法

知奕奕   2023-02-02 我要评论

基础画中画

manifest 设置

为了适配开启画中画状态时窗口的大小尺寸变化合理,我们需要修改 activity 中的对应属性

请为您的主 activity 添加如下属性

  • configChanges 当 activity 尺寸变化是走出适配
  • launchMode 若使用画中画,则必须单任务执行
  • resizeableActivity 确保可以重新调节 activity 尺寸
  • supportsPictureInPicture 开启画中画支持
<activity
    android:name=".MainActivity"
    android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
    android:exported="true"
    android:launchMode="singleTask"
    android:resizeableActivity="true"
    android:supportsPictureInPicture="true">
    <meta-data
        android:name="android.app.lib_name"
        android:value="" />
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

布局

即一线性布局,配上 videoview,使他充满整个屏幕宽高

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <VideoView
        android:id="@+id/video"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

开启画中画

定义一个开启画中画的方法 minimize

private fun minimize() {
    // 画中画builder
    var builder = PictureInPictureParams.Builder()
    // rational设定尺寸大小
    val info = Rational(video.width, video.height)
    builder.setAspectRatio(info).build()
    // 开启画中画
    enterPictureInPictureMode(builder.build())
}

为了简化使用,我们定义:在按下导航栏的 home 键时,整个 activity 缩小成画中画形式,并仅展示 videoview

这一步骤可以通过重写 onUserLeaveHint 方法实现

override fun onUserLeaveHint() {
    minimize()
}

上传一个你喜欢的视频,插入组件,运行程序即可

目前还未做 UI 优化,所以整体结构还是很丑

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们