温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android画中画窗口如何开启

发布时间:2023-01-05 09:42:10 来源:亿速云 阅读:146 作者:iii 栏目:开发技术

这篇文章主要讲解了“Android画中画窗口如何开启”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android画中画窗口如何开启”吧!

基础画中画

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()
}

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

感谢各位的阅读,以上就是“Android画中画窗口如何开启”的内容了,经过本文的学习后,相信大家对Android画中画窗口如何开启这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI