Android畫中畫窗口開啟方法
基礎(chǔ)畫中畫
manifest 設(shè)置
為了適配開啟畫中畫狀態(tài)時窗口的大小尺寸變化合理,我們需要修改 activity 中的對應(yīng)屬性
請為您的主 activity 添加如下屬性
- configChanges 當(dāng) activity 尺寸變化是走出適配
- launchMode 若使用畫中畫,則必須單任務(wù)執(zhí)行
- resizeableActivity 確保可以重新調(diào)節(jié) 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設(shè)定尺寸大小 val info = Rational(video.width, video.height) builder.setAspectRatio(info).build() // 開啟畫中畫 enterPictureInPictureMode(builder.build()) }
為了簡化使用,我們定義:在按下導(dǎo)航欄的 home 鍵時,整個 activity 縮小成畫中畫形式,并僅展示 videoview
這一步驟可以通過重寫 onUserLeaveHint
方法實現(xiàn)
override fun onUserLeaveHint() { minimize() }
上傳一個你喜歡的視頻,插入組件,運行程序即可
目前還未做 UI 優(yōu)化,所以整體結(jié)構(gòu)還是很丑
到此這篇關(guān)于Android畫中畫窗口開啟方法的文章就介紹到這了,更多相關(guān)Android畫中畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android studio集成unity導(dǎo)出工程的實現(xiàn)
本文主要介紹了android studio集成unity導(dǎo)出工程的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05android中GridView實現(xiàn)點擊查看更多功能示例
本篇文章主要介紹了android中GridView實現(xiàn)點擊查看更多功能示例,非常具有實用價值,需要的朋友可以參考下。2017-02-02基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出
這篇文章主要介紹了基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出的相關(guān)資料,需要的朋友可以參考下2015-12-12