title: FrameLayout is shrinking ImageView , but camera is opening properly inside FrameLayout
body: <p>I am opening the android Camera inside a FrameLayout in landscape mode, then the screen looks  like-</p>
  
  <p><img src="https://i.stack.imgur.com/cnJlS.png" alt="enter image description here"></p>
  
  <p>But the same Image when I open inside ImageView in FrameLayout, then it looks like-
  <img src="https://i.stack.imgur.com/6MkOY.png" alt="enter image description here"></p>
  
  <p>Please ignore the content of the Picture and funny squares drawn in the second image.
  The difference is,
  First CameraView opened in the entire screen or height touched the head of thescreen but second image shirinking the image to fit.</p>
  
  <p>I want the First image as well work like second, it should fit under a defined rectange. The only objective is Camera View and ImageView shows the same Look.</p>
  
  <p>I can change the entire layout as well if required.
  FacePreviewImageView is the image I am adding.</p>
  
  <p>First Camera View is Android Camera only and I am adding into the android framelayout-</p>
  
  <pre><code>Camera mCamera = Camera.open(1);
  FrameLayout layout = (FrameLayout) findViewById(R.id.ll2);
  layout.addView(new CameraView());
  </code></pre>
  
  <p>Second Frame I am adding like-</p>
  
  <pre><code>layout.removeAllViews();
                  layout.addView(faceImageView);
                  faceView.setVisibility(View.GONE);
                  faceImageView.setVisibility(View.VISIBLE);
  </code></pre>
  
  <p>The Layout XML is -</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" 
      android:baselineAligned="false"&gt;
  
      &lt;LinearLayout
          android:id="@+id/ll1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1" 
          android:orientation="horizontal"&gt;
  
  
      &lt;/LinearLayout&gt;
  
       &lt;FrameLayout
          android:id="@+id/ll2"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="0.5"
          android:orientation="horizontal" &gt;
  
           &lt;com.example.defaultfacetracker.FacePreviewImageView
               android:id="@+id/facePreview"             
               android:layout_width="match_parent"
               android:layout_height="match_parent" 
               android:src="@android:drawable/toast_frame"
               /&gt; 
  
      &lt;/FrameLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/ll3"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:text="Process" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My SurfaceHolder looks like-</p>
  
  <pre><code>class Preview extends SurfaceView implements SurfaceHolder.Callback {
      SurfaceHolder mHolder;
      Camera mCamera;
      Camera.PreviewCallback previewCallback;
  
      Preview(Context context, Camera.PreviewCallback previewCallback, Camera mCamera2) {
          super(context);
          this.previewCallback = previewCallback;
          this.mCamera = mCamera2;
  
          // Install a SurfaceHolder.Callback so we get notified when the
          // underlying surface is created and destroyed.
          mHolder = getHolder();
          mHolder.addCallback(this);
          mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      }
  
      public void surfaceCreated(SurfaceHolder holder) {
          // The Surface has been created, acquire the camera and tell it where
          // to draw.
        //  mCamera = Camera.open(1);
          //mCamera.setDisplayOrientation(270);
          try {
             mCamera.setPreviewDisplay(holder);
          } catch (IOException exception) {
              mCamera.release();
              mCamera = null;
              // TODO: add more exception handling logic here
          }
      }
  
      public void surfaceDestroyed(SurfaceHolder holder) {
          // Surface will be destroyed when we return, so stop the preview.
          // Because the CameraDevice object is not a shared resource, it's very
          // important to release it when the activity is paused.
          mCamera.stopPreview();
          mCamera.release();
          mCamera = null;
      }
  
  
      private Size getOptimalPreviewSize(List&lt;Size&gt; sizes, int w, int h) {
          final double ASPECT_TOLERANCE = 0.05;
          double targetRatio = (double) w / h;
          if (sizes == null) return null;
  
          Size optimalSize = null;
          double minDiff = Double.MAX_VALUE;
  
          int targetHeight = h;
  
          // Try to find an size match aspect ratio and size
          for (Size size : sizes) {
              double ratio = (double) size.width / size.height;
              if (Math.abs(ratio - targetRatio) &gt; ASPECT_TOLERANCE) continue;
              if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                  optimalSize = size;
                  minDiff = Math.abs(size.height - targetHeight);
              }
          }
  
          // Cannot find the one match the aspect ratio, ignore the requirement
          if (optimalSize == null) {
              minDiff = Double.MAX_VALUE;
              for (Size size : sizes) {
                  if (Math.abs(size.height - targetHeight) &lt; minDiff) {
                      optimalSize = size;
                      minDiff = Math.abs(size.height - targetHeight);
                  }
              }
          }
          return optimalSize;
      }
  
      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
          // Now that the size is known, set up the camera parameters and begin
          // the preview.
          Camera.Parameters parameters = mCamera.getParameters();
  
          List&lt;Size&gt; sizes = parameters.getSupportedPreviewSizes();
          Size optimalSize = getOptimalPreviewSize(sizes, w, h);
          parameters.setPreviewSize(optimalSize.width, optimalSize.height);
  
          mCamera.setParameters(parameters);
          if (previewCallback != null) {
              mCamera.setPreviewCallbackWithBuffer(previewCallback);
              Camera.Size size = parameters.getPreviewSize();
              byte[] data = new byte[size.width*size.height*
                      ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())/8];
              mCamera.addCallbackBuffer(data);
          }
  //        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  //          mCamera.setDisplayOrientation(90);
  //          
  //      } else {
  //          mCamera.setDisplayOrientation(0);
  //      }
          mCamera.startPreview();
      }
  
  }
  </code></pre>