实际上,您可以使用相机参数来控制相机预览大小,例如我在相机预览中使用的参数
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(holder);
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
Preview.this.invalidate();
}
});
LayoutParams lparam = this.getLayoutParams();
lparam.height = this.getHeight();
lparam.width = parameters.getPreviewSize().width;
parameters.setPreviewSize(lparam.width, lparam.height);
this.setLayoutParams(lparam);
} catch (IOException e) {
e.printStackTrace();
}
}
您还可以根据屏幕的高度和宽度修改高度,以便保持纵横比。您可以使用此功能来实现:
public static int getDisplayWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
return width;
}
public static int getDisplayHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int height = display.getHeight();
return height;
}
然后在上面的surfaceCreated中使用它来确定相机预览的宽高