Android画中画

基础画中画

manifest 设置

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

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

  1. configChanges 当 activity 尺寸变化是走出适配
  2. launchMode 若使用画中画,则必须单任务执行
  3. resizeableActivity 确保可以重新调节 activity 尺寸
  4. 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 优化,所以整体结构还是很丑


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zhillery

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值