title: Android and SurfaceView. Wrong size
body: <p>I have some problems with SurfaceView resizing in Android 2.3. versions. I am working with video player I need to resize my SurfaceView when video frame size doesn't match surface size. I have Fragment with SurfaceView inside it. Fragment doesnt match full screen, just some part of it. For Android 4 versions everything is OK, but in Android 2.3. after resizing my SurfaceView is bigger than its FrameLayout parent. I have no ideas why this is happening.</p>
  
  <p>Here is interface which allowas video size init</p>
  
  <pre><code>/**
   * This method is called by native vout to request a surface resize when frame size doesn't match surface size.
   * @param width Frame width
   * @param height Frame height
   * @param visible_width Visible frame width
   * @param visible_height Visible frame height
   * @param sar_num Surface aspect ratio numerator
   * @param sar_den Surface aspect ratio denominator
   */
  void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den);
  
  //This method is an implementation of Interface
  @Override
  public void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {
      if (width * height == 0)
          return;
  
      // store video size
      mVideoHeight = height;
      mVideoWidth = width;
      mVideoVisibleHeight = visible_height;
      mVideoVisibleWidth  = visible_width;
      mSarNum = sar_num;
      mSarDen = sar_den;
      Message msg = mHandler.obtainMessage(SURFACE_SIZE);
      mHandler.sendMessage(msg);
  }
  </code></pre>
  
  <p>And the second - change my SurfaceView size.</p>
  
  <pre><code> private void changeSurfaceSize() {
      int sw;
      int sh;
  
      // get screen size
      if (mPresentation == null) {
          Log.d("MyLog", "Presentation is null");
          sw = ((Activity)mainActivity).getWindow().getDecorView().getWidth();
          sh = ((Activity)mainActivity).getWindow().getDecorView().getHeight();
      } else {
          Log.d("MyLog", "Presentation is not null");
          sw = mPresentation.getWindow().getDecorView().getWidth();
          sh = mPresentation.getWindow().getDecorView().getHeight();
      }
  
      double dw = sw, dh = sh;
      boolean isPortrait;
  
      if (mPresentation == null) {
          // getWindow().getDecorView() doesn't always take orientation into account, we have to correct the values
          isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
      } else {
          isPortrait = false;
      }
  
      if (sw &gt; sh &amp;&amp; isPortrait || sw &lt; sh &amp;&amp; !isPortrait) {
          dw = sh;
          dh = sw;
      }
  
      // sanity check
      if (dw * dh == 0 || mVideoWidth * mVideoHeight == 0) {
          Log.e(TAG, "Invalid surface size");
          return;
      }
  
      // compute the aspect ratio
      double ar, vw;
      double density = (double)mSarNum / (double)mSarDen;
      if (density == 1.0) {
          /* No indication about the density, assuming 1:1 */
          vw = mVideoVisibleWidth;
          ar = (double)mVideoVisibleWidth / (double)mVideoVisibleHeight;
      } else {
          /* Use the specified aspect ratio */
          vw = mVideoVisibleWidth * density;
          ar = vw / mVideoVisibleHeight;
      }
  
      // compute the display aspect ratio
      double dar = dw / dh;
  
      switch (mCurrentSize) {
          case SURFACE_BEST_FIT:
              if (dar &lt; ar)
                  dh = dw / ar;
              else
                  dw = dh * ar;
              break;
          case SURFACE_FIT_HORIZONTAL:
              dh = dw / ar;
              break;
          case SURFACE_FIT_VERTICAL:
              dw = dh * ar;
              break;
          case SURFACE_FILL:
              break;
          case SURFACE_16_9:
              ar = 16.0 / 9.0;
              if (dar &lt; ar)
                  dh = dw / ar;
              else
                  dw = dh * ar;
              break;
          case SURFACE_4_3:
              ar = 4.0 / 3.0;
              if (dar &lt; ar)
                  dh = dw / ar;
              else
                  dw = dh * ar;
              break;
          case SURFACE_ORIGINAL:
              dh = mVideoVisibleHeight;
              dw = vw;
              break;
      }
  
      SurfaceView surface;
      SurfaceView subtitlesSurface;
      SurfaceHolder surfaceHolder;
      SurfaceHolder subtitlesSurfaceHolder;
      FrameLayout surfaceFrame;
  
      if (mPresentation == null) {
          surface = mSurface;
          subtitlesSurface = mSubtitlesSurface;
          surfaceHolder = mSurfaceHolder;
          subtitlesSurfaceHolder = mSubtitlesSurfaceHolder;
          surfaceFrame = mSurfaceFrame;
      } else {
          surface = mPresentation.mSurface;
          subtitlesSurface = mPresentation.mSubtitlesSurface;
          surfaceHolder = mPresentation.mSurfaceHolder;
          subtitlesSurfaceHolder = mPresentation.mSubtitlesSurfaceHolder;
          surfaceFrame = mPresentation.mSurfaceFrame;
      }
  
      // force surface buffer size
      surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
      subtitlesSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
  
      // set display size
      LayoutParams lp = surface.getLayoutParams();
      lp.width  = (int) Math.ceil(dw * mVideoWidth / mVideoVisibleWidth);
      lp.height = (int) Math.ceil(dh * mVideoHeight / mVideoVisibleHeight);
      surface.setLayoutParams(lp);
      subtitlesSurface.setLayoutParams(lp);
  
      // set frame size (crop if necessary)
      lp = surfaceFrame.getLayoutParams();
      lp.width = (int) Math.floor(dw);
      lp.height = (int) Math.floor(dh);
      surfaceFrame.setLayoutParams(lp);
  
      surface.invalidate();
      subtitlesSurface.invalidate();
  }
  </code></pre>
  
  <p>Does anybody have any ideas, why could it happens so? I will be very glad for any help!!! Thank you!</p>