经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android画中画窗口开启方法
来源:jb51  时间:2023/1/6 8:52:26  对本文有异议

基础画中画

manifest 设置

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

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

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

布局

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <VideoView
  8. android:id="@+id/video"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" />
  11.  
  12. </LinearLayout>

开启画中画

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

  1. private fun minimize() {
  2. // 画中画builder
  3. var builder = PictureInPictureParams.Builder()
  4. // rational设定尺寸大小
  5. val info = Rational(video.width, video.height)
  6. builder.setAspectRatio(info).build()
  7. // 开启画中画
  8. enterPictureInPictureMode(builder.build())
  9. }

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

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

  1. override fun onUserLeaveHint() {
  2. minimize()
  3. }

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

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

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号