title: Android Camera Preview Stretched
body: <p>I've been working on making my custom camera activity on Android, but when rotating the camera, the aspect ratio of the surface view gets messed up.</p>
  
  <p>In my oncreate for the activity, I set the framelayout which holds the surface view that displays the camera's parameters.</p>
  
  <pre><code>//FrameLayout that will hold the camera preview
          FrameLayout previewHolder = (FrameLayout) findViewById(R.id.camerapreview);
  
          //Setting camera's preview size to the best preview size
          Size optimalSize = null;
          camera = getCameraInstance();
          double aspectRatio = 0;
          if(camera != null){
              //Setting the camera's aspect ratio
              Camera.Parameters parameters = camera.getParameters();
              List&lt;Size&gt; sizes = parameters.getSupportedPreviewSizes();
              optimalSize = CameraPreview.getOptimalPreviewSize(sizes, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
              aspectRatio = (float)optimalSize.width/optimalSize.height;
          }
  
          if(optimalSize!= null){
              RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int)(getResources().getDisplayMetrics().widthPixels*aspectRatio));
              previewHolder.setLayoutParams(params);
              LayoutParams surfaceParams = new LayoutParams(LayoutParams.MATCH_PARENT, (int)(getResources().getDisplayMetrics().widthPixels*aspectRatio));
              cameraPreview.setLayoutParams(surfaceParams);
  
          }
  
          cameraPreview.setCamera(camera);
  
          //Adding the preview to the holder
          previewHolder.addView(cameraPreview);
  </code></pre>
  
  <p>Then, in the Surface view I set the camera's parameters to be displayed</p>
  
  <pre><code>public void setCamera(Camera camera) {
          if (mCamera == camera) { return; }
  
          mCamera = camera;
  
          if (mCamera != null) {
              requestLayout();
  
              try {
                  mCamera.setPreviewDisplay(mHolder);
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
  
              if(mCamera != null){
                  //Setting the camera's aspect ratio
                  Camera.Parameters parameters = mCamera.getParameters();
                  List&lt;Size&gt; sizes = parameters.getSupportedPreviewSizes();
                  Size optimalSize = getOptimalPreviewSize(sizes, getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
  
                  parameters.setPreviewSize(optimalSize.width, optimalSize.height);
                  mCamera.setParameters(parameters);
              }
  
              /*
                Important: Call startPreview() to start updating the preview surface. Preview must 
                be started before you can take a picture.
                */
              mCamera.startPreview();
          }
  
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/4AS4V.png" alt="enter image description here"></p>
  
  <p>You can see that the LEGO man grows taller and skinnier when the phone is rotated:</p>
  
  <p><img src="https://i.stack.imgur.com/tp0CY.png" alt=""></p>
  
  <p><strong>How can I ensure that the aspect ratio for my camera view is correct?</strong></p>