SurfaceView height + width gets ignored

后端 未结 2 999
猫巷女王i
猫巷女王i 2020-12-18 10:52

The following script works flawless, but the size of my videoplayer will get f\'d after changing my surfaceview from portrait to landscape. Tried several options like

2条回答
  •  悲&欢浪女
    2020-12-18 11:33

    use this method and call it inside onConfigurationChanged method :

    private void setVideoSize() {
    
        // // Get the dimensions of the video
    
                // mVideoView is your video view object.
    
        int videoWidth = mVideoView.getVideoWidth();
        int videoHeight = mVideoView.getVideoHeight();
        float videoProportion = (float) videoWidth / (float) videoHeight;
    
        // Get the width of the screen
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        float screenProportion = (float) screenWidth / (float) screenHeight;
    
        // Get the SurfaceView layout parameters
        android.view.ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
        if (videoProportion > screenProportion) {
            lp.width = screenWidth;
            lp.height = (int) ((float) screenWidth / videoProportion);
        } else {
            lp.width = (int) (videoProportion * (float) screenHeight);
            lp.height = screenHeight;
        }
        // Commit the layout parameters
        mVideoView.setLayoutParams(lp);
    }
    

    Now on change of orientation your videoview will be resized.

提交回复
热议问题