/**
 * A layout which handles the preview aspect ratio and the position of
 * the gripper.
 */
public class PreviewFrameLayout extends ViewGroup {
private static final int MIN_HORIZONTAL_MARGIN = 0; // 10dp /** A callback to be invoked when the preview frame's size changes. */
public interface OnSizeChangedListener {
public void onSizeChanged();
} private double mAspectRatio = 3.0 / 4.0; // 宽 : 高
private FrameLayout mFrame;
private OnSizeChangedListener mSizeListener;
private final DisplayMetrics mMetrics = new DisplayMetrics(); public PreviewFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
((Activity) context).getWindowManager()
.getDefaultDisplay().getMetrics(mMetrics);
} public void setOnSizeChangedListener(OnSizeChangedListener listener) {
mSizeListener = listener;
} @Override
protected void onFinishInflate() {
mFrame = (FrameLayout) findViewById(R.id.frame);
if (mFrame == null) {
throw new IllegalStateException(
"must provide child with id as \"frame\"");
}
} public void setAspectRatio(double ratio) {
if (ratio <= 0.0) throw new IllegalArgumentException(); if (mAspectRatio != ratio) {
mAspectRatio = ratio;
requestLayout();
}
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Try to layout the "frame" in the center of the area, and put
// "gripper" just to the left of it. If there is no enough space for
// the gripper, the "frame" will be moved a little right so that
// they won't overlap with each other. int frameWidth = getWidth();
int frameHeight = getHeight(); FrameLayout f = mFrame; int horizontalPadding = f.getPaddingLeft() + f.getPaddingRight();
int verticalPadding = f.getPaddingTop() + f.getPaddingBottom(); // Ignore the vertical paddings, so that we won't draw the frame on the
// top and bottom sides
int previewHeight = frameHeight;
int previewWidth = frameWidth; // resize frame and preview for aspect ratio
int idealPreviewWidth = (int)(previewHeight * mAspectRatio);
if (previewWidth > idealPreviewWidth ) {
previewWidth = (int) (idealPreviewWidth + .5);
} else {
previewHeight = (int) (previewWidth / mAspectRatio + .5);
} frameWidth = previewWidth + horizontalPadding;
frameHeight = previewHeight + verticalPadding; int hSpace = ((r - l) - frameWidth) / 2;
int vSpace = ((b - t) - frameHeight) / 2;
mFrame.measure(
MeasureSpec.makeMeasureSpec(frameWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(frameHeight, MeasureSpec.EXACTLY));
mFrame.layout(l + hSpace, t + vSpace, r - hSpace, b - vSpace);
if (mSizeListener != null) {
mSizeListener.onSizeChanged();
}
}
}
Size size0 = parameters.getPictureSize();
if( rotation == 0 || rotation == 2 ) {
mPreviewFL.setAspectRatio((double) size0.height / size0.width);

nWidth = width;
nHeight = height;
} else if( rotation == 1 || rotation == 3) {
mPreviewFL.setAspectRatio((double) size0.width / size0.height);

nWidth = width;
nHeight = height;
}

解决方案 »

  1.   

    汗,我怎么还没编辑完,就自动提交了呢?在做Camera的PreviewSize时,必须考虑包含Camera的SurfaceView的父窗口的 “ 宽高比“。但是getPictureSize返回的总是"宽 > 高"一个尺寸。
    同时我们还要考虑摄像头默认方向;旋转角度;系统预定义的预览尺寸;父窗口大小
    已经旋转切换。这些很复杂了。
      

  2.   

    你好,我也遇到这样的问题,用不同的平板测,int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();得到的rotation是不一样的,宽的一边有的是0,2,有的是1,3。好像分默认水平和默认竖直的,不知道改怎么判断系统默认方向(这个值应该是固定的)?求解答
      

  3.   

    原来是CameraInfo cameraInfo = new CameraInfo;      cameraInfo.oriention啊