Code example for SurfaceView

Methods: getHeight, getLayoutParams, getWidth

0
    } 
 
    MediaPlayer.OnVideoSizeChangedListener mVideoSizeListener = new MediaPlayer.OnVideoSizeChangedListener() {
        @Override 
        public void onVideoSizeChanged(MediaPlayer mediaPlayer, int width, int height) { 
            int surfaceHeight = mVideoPreview.getHeight(); 
            int surfaceWidth = mVideoPreview.getWidth(); 
 
            android.view.ViewGroup.LayoutParams lp = mVideoPreview.getLayoutParams(); 
 
            int newHeight = (int) ((double) surfaceWidth * ((double) height / (double) width)); 
            if (newHeight <= surfaceHeight) { 
                lp.height = newHeight; 
            } 
            else { 
                lp.width = (int) ((double) surfaceHeight * ((double) width / (double) height)); 
            } 
        } 
    }; 
 
    /**