Android畫中畫窗口開啟方法
基礎(chǔ)畫中畫
manifest 設(shè)置
為了適配開啟畫中畫狀態(tài)時窗口的大小尺寸變化合理,我們需要修改 activity 中的對應(yīng)屬性
請為您的主 activity 添加如下屬性
- configChanges 當 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之rk3588?開發(fā)環(huán)境準備及問題解決方法
這篇文章主要介紹了Android中的rk3588?開發(fā)環(huán)境準備,本文給大家分享遇到的問題及解決方法,本文給大家講解的非常詳細對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11
Android穩(wěn)定性:可遠程配置化的Looper兜底框架
這篇文章主要為大家介紹了Android穩(wěn)定性可遠程配置化的Looper兜底框架實例實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
android notification 的總結(jié)分析
notification是一種出現(xiàn)在任務(wù)欄的提示,特別是在4.0以后notification改進了不少,本文內(nèi)容都是基于4.0及4.1以后總結(jié)來的2013-05-05

