我还没有找到任何方法来裁剪相机预览,然后将其显示在 SurfaceView 上。
Android - 是否可以裁剪相机预览?
最佳答案
您可以在没有叠加 View 的情况下执行此操作(并非在所有情况下都有效)。
子类 ViewGroup,添加 SurfaceView 作为唯一的 child ,然后:
- 在 onMeasure 中提供您想要的裁剪尺寸。
- 在 onLayout 中,使用未裁剪的尺寸布局 SurfaceView。
基本上,
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 );
}
}
}
关于android - 是否可以裁剪相机预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601982/