日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android畫中畫窗口開啟方法_Android

作者:知奕奕 ? 更新時間: 2023-02-12 編程語言

基礎畫中畫

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 優化,所以整體結構還是很丑

原文鏈接:https://blog.csdn.net/delete_you/article/details/127730705

欄目分類
最近更新