/**
* 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;
}
解决方案 »
- ViewPager中的gridview怎么更新数据呢,求高人。。。
- 怎么结束一个service里启动的activity?
- 如何获取LightsService,
- checkbox 值回填并且选中怎么选中不了啊!给分啊!
- 如何打开androidSDK中的DEMO
- 关于canvas.drawtext()
- 如何编译源码生成的刷机包是bin格式的?
- 自己用android写的程序,怎么能让它根据可用的网络自动转换
- 请教SDK Manager 下的各版本的api下的ARM EABI v7a System Image 和google apis 都没有显示
- android 项目开发中出错了
- 请教各位android照片显示的问题
- Android中list.add问题
同时我们还要考虑摄像头默认方向;旋转角度;系统预定义的预览尺寸;父窗口大小
已经旋转切换。这些很复杂了。