Kotlin--實現視訊播放全屏
-
AndroidMainfest.xml
android:screenOrientation="landscape"
實現全屏的第一個步驟,就是要把螢幕切換為橫屏狀態,這裡我們直接在
AndroidMainfest.xml
為Activity 設定為橫屏模式android:screenOrientation="landscape"
。<activity android:name=".other.MediaPlayerActivity" android:screenOrientation="landscape"/>
此時的介面狀態如下:
初始狀態
2、去掉狀態列和導航條,在
MediaPlayerActivity
重寫onWindowFocusChanged
方法,程式碼如下:
super.onWindowFocusChanged(hasFocus) if (hasFocus && Build.VERSION.SDK_INT >= 19) { window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY } }
由於android5.0才支援去掉狀態列,所以我們在這裡做了個判斷。
此時介面如下:

去掉導航欄和狀態列
VideoView
的
onMeasure
方法,達到全屏效果。程式碼如下:
class CustomVideoView : VideoView { constructor(context: Context) : this(context, null) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { var width = getDefaultSize(getScreenPoint().x, widthMeasureSpec) var height = getDefaultSize(getScreenPoint().y, heightMeasureSpec) setMeasuredDimension(width, height); } }
在佈局在使用:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.zhangwenshuan.palyer.CustomVideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
那麼,效果就實現了:

最終效果
第一次全屏的時候,系統會提示從螢幕下滑可以顯示導航條。避免不知道怎麼返回。
4、這是最重要一步,關注公眾號,一起進步。

用時間,換天分