可以使用SurfaceView来实现相机预览,并在上面绘制半透明的矩形,将需要完全透明的区域设置为透明。代码如下所示:
private SurfaceView mSurfaceView; ... mSurfaceView = findViewById(R.id.surface_view);
private SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { try { CameraManager.getInstance().openCamera(Camera.CameraInfo.CAMERA_FACING_BACK, holder); } catch (Exception e) { e.printStackTrace(); } }
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override public void surfaceDestroyed(SurfaceHolder holder) { CameraManager.getInstance().releaseCamera(); } }; ... mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);
private void drawTransparentRect() { Canvas canvas = mTransparentView.getHolder().lockCanvas(); if (canvas == null) { return; } Paint paint = new Paint(); paint.setColor(Color.TRANSPARENT); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode