Kotlin--实现视频播放全屏

作者: GitCode8 | 来源:发表于2018-10-24 18:16 被阅读11次
    1. 实现全屏的第一个步骤,就是要把屏幕切换为横屏状态,这里我们直接在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才支持去掉状态栏,所以我们在这里做了个判断。
    此时界面如下:

    去掉导航栏和状态栏
    3、通过第2步,我们知道界面底部有一条白色的横条,为此,我们通过重写VideoViewonMeasure方法,达到全屏效果。代码如下:
    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、这是最重要一步,关注公众号,一起进步。


    用时间,换天分

    相关文章

      网友评论

        本文标题:Kotlin--实现视频播放全屏

        本文链接:https://www.haomeiwen.com/subject/ruuxtqtx.html