title: Resizing surface view for aspect ratio change in video display in android
body: <p>I am working on a video conferencing project. My video display is using surface view. Now during a video call there is a chance of aspect ratio change for the incoming frames. So i have tried the following code for it</p>
  
  <pre><code>public void surfaceResize() {
  
     // WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      Point size = new Point();
      int screenWidth = 0;
      //Get the SurfaceView layout parameters
  
      float aspectRatio = (float) recv_frame_width / recv_frame_height;
  
      if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) 
      {   
          //Get the width of the screen
          getWindowManager().getDefaultDisplay().getSize(size);
          screenWidth = size.x;
  
          //Set the width of the SurfaceView to the width of the screen
          surflp.width = screenWidth;
  
          //Set the height of the SurfaceView to match the aspect ratio of the video 
          //be sure to cast these as floats otherwise the calculation will likely be 0
          surflp.height = (int) ((1 / aspectRatio) * (float)screenWidth);
  
          //Commit the layout parameters
  
      } else {
  
          size.x = size.y = 0;
          //Get the width of the screen
          getWindowManager().getDefaultDisplay().getSize(size);
  
          int screenHeight = size.y;
  
          //Set the width of the SurfaceView to the width of the screen
          surflp.height = screenHeight;
  
          //Set the width of the SurfaceView to match the aspect ratio of the video 
          //be sure to cast these as floats otherwise the calculation will likely be 0
          surflp.width = (int) ( aspectRatio * (float)screenHeight);
  
          //Commit the layout parameters
          // code to do for Portrait Mode        
      }
      surflp.addRule(RelativeLayout.CENTER_HORIZONTAL);
      surflp.addRule(RelativeLayout.CENTER_VERTICAL);
  
      if(myVideoSurfaceView != null) 
          myVideoSurfaceView.setLayoutParams(surflp);  
      System.out.println("Surface resized*****************************************");
  }
  </code></pre>
  
  <p>Everything is fine if I call this function at beginning of call. 
  My problem is when I call this function in between a call for changing aspect ratio it takes too much time to display the next frame. Sometimes the video gets stuck even.</p>
  
  <p>I tried to destroy and recreate the surface with</p>
  
  <pre><code>myVideoSurface.setVisibility(VIEW.GONE);
  </code></pre>
  
  <p>But the surface is not getting created.</p>
  
  <p>I am using Mediacodec for video decode I will get notified when there is a resolution change.</p>
  
  <p>Is there something more I should do for resizing a surfaceView when already video is being played.</p>
  
  <p>Thanks for help.........................</p>