1. 程式人生 > 其它 >頁面中間視訊橫豎屏播放問題處理

頁面中間視訊橫豎屏播放問題處理

技術標籤:小瑕疵

序 、好久沒有寫部落格了,最近在忙商業的“大事” 。世間的道理就是這樣:有用的都會錯 ,不會錯的都沒用 。所以你要出來做事就會犯錯 ,你要想不犯錯,就只剩下到的優越感 。

上個版本要在題庫的做題頁面加一個視訊解析功能 ,可以看視訊 ,可以當前頁面橫豎屏播放 。由於時間緊迫 ,當時跟產品對了一下重新實現頁面效果 ,播放視訊跳轉到新的頁面 。難度降了N個等級 。這不,上完線之後開始來優化遺留的技術債務 。

效果圖如下 。

播放視訊視窗在頁面中間 ,橫屏的時候也就是讓頁面全屏播放 ,簡單理解就是設定 MATCH_PARENT 。由於層級問題所以直接設定的話是不可以的 ,可以試一下看看效果 。然後參考了GSYVideolayer 的實現邏輯 。



程式碼如下:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (fl_video_container == null) fl_video_container = findViewById(R.id.fl_video_container);
        if (parent_video == null) parent_video = findViewById(R.id.parent_video);
        if (viewGroup == null) viewGroup = findViewById(Window.ID_ANDROID_CONTENT);
        final ConstraintLayout.LayoutParams lpParent = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) parent_video.getLayoutParams();

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 此時為橫屏
            mIsFullScreen = true;
            ViewGroup.LayoutParams layoutParams = fl_video_container.getLayoutParams();
            layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
            params.width = LinearLayout.LayoutParams.MATCH_PARENT;
            params.height = LinearLayout.LayoutParams.MATCH_PARENT;

            frameLayout.setBackgroundColor(Color.BLACK);
            fl_video_container.setLayoutParams(layoutParams);
            parent_video.removeView(fl_video_container);
            ViewParent parent = fl_video_container.getParent();
            frameLayout.addView(fl_video_container);
            viewGroup.addView(frameLayout, lpParent);
        } else {
            mIsFullScreen = false;
            ViewGroup.LayoutParams layoutParams = fl_video_container.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = getResources().getDimensionPixelSize(R.dimen.xdp_220);
            params.width = LinearLayout.LayoutParams.MATCH_PARENT;
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT;

            frameLayout.setBackgroundColor(Color.TRANSPARENT);
            frameLayout.removeView(fl_video_container);
            viewGroup.removeView(frameLayout);
            fl_video_container.setLayoutParams(layoutParams);
            parent_video.setLayoutParams(params);
            parent_video.addView(fl_video_container);
            ConstraintLayout.LayoutParams params1 = (ConstraintLayout.LayoutParams) fl_video_container.getLayoutParams();
            params1.topToBottom = R.id.tv_video_title;
            params1.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
            params1.leftMargin = getResources().getDimensionPixelSize(R.dimen.xdp_15);
            params1.rightMargin = getResources().getDimensionPixelSize(R.dimen.xdp_15);
            params1.topMargin = getResources().getDimensionPixelSize(R.dimen.xdp_10);
            fl_video_container.setLayoutParams(params1);
        }
    }

原理

實現原理是 ,把視訊放在最頂層的容器 (視訊設定 MATCH_PARENT)。

viewGroup = findViewById(Window.ID_ANDROID_CONTENT);

END、告辭,繼續去碼程式碼 。