diff --git a/res/layout-xlarge-land/dialer.xml b/res/layout-xlarge-land/dialer.xml index 10cd6f1f9..725839d8d 100644 --- a/res/layout-xlarge-land/dialer.xml +++ b/res/layout-xlarge-land/dialer.xml @@ -4,7 +4,7 @@ android:layout_height="fill_parent" android:gravity="bottom|center_horizontal"> - - mSupportedSizes; + Camera mCamera; + + public void setCamera(Camera camera) { + mCamera = camera; + if (mCamera != null) { + mSupportedSizes = mCamera.getParameters().getSupportedPreviewSizes(); + requestLayout(); + } + } + + public void switchCamera(Camera camera) { + setCamera(camera); + try { + camera.setPreviewDisplay(mHolder); + } catch (IOException exception) { + + } + Camera.Parameters parameters = camera.getParameters(); + parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); + requestLayout(); + + camera.setParameters(parameters); + } + + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);; + int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);; + setMeasuredDimension(width, height); + + if (mSupportedSizes != null) { + mPreviewSize = getOptimalPreviewSize(mSupportedSizes, width, height); + } + Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) { + Size tempSize = mPreviewSize; + mPreviewSize.width = tempSize.height; + mPreviewSize.height = tempSize.width; + } else { + + } + } + + protected void onLayout(boolean changed, int l, int t, int r, int b) { + if (changed && getChildCount() > 0) { + final View child = getChildAt(0); + + final int width = r - l; + final int height = b - t; + + int previewWidth = width; + int previewHeight = height; + if (mPreviewSize != null) { + previewWidth = mPreviewSize.width; + previewHeight = mPreviewSize.height; + } + + // Center the surface view + if (width * previewHeight > height * previewWidth) { + final int scaledChildWidth = previewWidth * height / previewHeight; + child.layout((width - scaledChildWidth) / 2, 0, + (width + scaledChildWidth) / 2, height); + } else { + final int scaledChildHeight = previewHeight * width / previewWidth; + child.layout(0, (height - scaledChildHeight) / 2, + width, (height + scaledChildHeight) / 2); + } + } + } + + public void surfaceCreated(SurfaceHolder holder) { + try { + if (mCamera != null) { + mCamera.setPreviewDisplay(holder); + } + } catch (IOException exception) { + + } + } + + public void surfaceDestroyed(SurfaceHolder holder) { + if (mCamera != null) { + mCamera.stopPreview(); + } + } + + private Size getOptimalPreviewSize(List sizes, int w, int h) { + final double ASPECT_TOLERANCE = 0.1; + double targetRatio = (double) w / h; + if (sizes == null) return null; + + Size optimalSize = null; + double minDiff = Double.MAX_VALUE; + + int targetHeight = h; + + for (Size size : sizes) { + double ratio = (double) size.width / size.height; + if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; + if (Math.abs(size.height - targetHeight) < minDiff) { + optimalSize = size; + minDiff = Math.abs(size.height - targetHeight); + } + } + + if (optimalSize == null) { + minDiff = Double.MAX_VALUE; + for (Size size : sizes) { + if (Math.abs(size.height - targetHeight) < minDiff) { + optimalSize = size; + minDiff = Math.abs(size.height - targetHeight); + } + } + } + return optimalSize; + } + + public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + mCamera.stopPreview(); + Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + Camera.Parameters parameters = mCamera.getParameters(); + + if(display.getRotation() == Surface.ROTATION_90) { + mCamera.setDisplayOrientation(270); + } + else if(display.getRotation() == Surface.ROTATION_270) { + mCamera.setDisplayOrientation(90); + } + else if (display.getRotation() == Surface.ROTATION_180) { + mCamera.setDisplayOrientation(180); + } + requestLayout(); + + mCamera.setParameters(parameters); + mCamera.startPreview(); + } +} \ No newline at end of file