如何禁用相机预览的拉伸?

How to disable stretching for camera preview?

当我将我的 SurfaceView 设置为我的相机预览的目标表面时(如 and-dev tutorial 相机 API 1),它将根据需要从相机拉伸图片以填充整个表面图片。我怎样才能禁用它?我想为没有来自相机的适当图像数据的区域设置 2 个黑色条带,而不是在所有表面上缩放图像。

使用内部 SurfaceView 创建自定义视图

@momo 在他的解决方案中报告 ():

public class CroppedCameraPreview extends ViewGroup {
  private SurfaceView cameraPreview;
  public CroppedCameraPreview( Context context ) {
    super( context );
    // i'd probably create and add the SurfaceView here, but it doesn't matter
  }
  @Override
  protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) {
    setMeasuredDimension( croppedWidth, croppedHeight );
  }
  @Override
  protected void onLayout( boolean changed, int l, int t, int r, int b ) {
    if ( cameraPreview != null ) {
      cameraPreview.layout( 0, 0, actualPreviewWidth, actualPreviewHeight );
    }
  }
}