private void changeSurfaceLayout( int videoWidth, int videoHeight, int videoVisibleWidth, int videoVisibleHeight, int sarNum, int sarDen) { int sw; int sh; // get screen size Activity activity = TvApp.getApplication().getCurrentActivity(); if (activity == null) return; // called during destroy sw = activity.getWindow().getDecorView().getWidth(); sh = activity.getWindow().getDecorView().getHeight(); double dw = sw, dh = sh; boolean isPortrait; isPortrait = mActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; if (sw > sh && isPortrait || sw < sh && !isPortrait) { dw = sh; dh = sw; } // sanity check if (dw * dh == 0 || videoWidth * videoHeight == 0) { TvApp.getApplication().getLogger().Error("Invalid surface size"); return; } // compute the aspect ratio double ar, vw; if (sarDen == sarNum) { /* No indication about the density, assuming 1:1 */ vw = videoVisibleWidth; ar = (double) videoVisibleWidth / (double) videoVisibleHeight; } else { /* Use the specified aspect ratio */ vw = videoVisibleWidth * (double) sarNum / sarDen; ar = vw / videoVisibleHeight; } // compute the display aspect ratio double dar = dw / dh; if (dar < ar) dh = dw / ar; else dw = dh * ar; // set display size ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams(); lp.width = (int) Math.ceil(dw * videoWidth / videoVisibleWidth); lp.height = (int) Math.ceil(dh * videoHeight / videoVisibleHeight); normalWidth = lp.width; normalHeight = lp.height; mSurfaceView.setLayoutParams(lp); if (hasSubtitlesSurface) mSubtitlesSurface.setLayoutParams(lp); // set frame size (crop if necessary) if (mSurfaceFrame != null) { lp = mSurfaceFrame.getLayoutParams(); lp.width = (int) Math.floor(dw); lp.height = (int) Math.floor(dh); mSurfaceFrame.setLayoutParams(lp); } TvApp.getApplication().getLogger().Debug("Surface sized " + mVideoWidth + "x" + mVideoHeight); mSurfaceView.invalidate(); if (hasSubtitlesSurface) mSubtitlesSurface.invalidate(); }