Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android画中画

Android画中画窗口开启方法

作者:知奕奕

Android8.0 Oreo(API Level26)允许活动启动画中画Picture-in-picture(PIP)模式。PIP是一种特殊类型的多窗口模式,主要用于视频播放。PIP模式已经可用于Android TV,而Android8.0则让该功能可进一步用于其他Android设备

基础画中画

manifest 设置

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

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

<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 优化,所以整体结构还是很丑

到此这篇关于Android画中画窗口开启方法的文章就介绍到这了,更多相关Android画中画内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文