CameraPreview泛滥了parentView

时间:2015-11-10 12:58:54

标签: android android-camera

我有比率为16:9的camerapreview,我使用此代码进行裁剪:

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (changed && getChildCount() > 0) {
        final View child = getChildAt(0);

        final int width = r - l;
        final int height = b - t;

        int previewWidth = width;
        int previewHeight = height;
        if (mPreviewSize != null) {
            previewWidth = mPreviewSize.height;
            previewHeight = mPreviewSize.width;
        }

        // Center the child SurfaceView within the parent.
        if (width * previewHeight < height * previewWidth) {
            final int scaledChildWidth = previewWidth * height / previewHeight;
            child.layout((width - scaledChildWidth) / 2, 0,
                    (width + scaledChildWidth) / 2, height);
        } else {
            final int scaledChildHeight = previewHeight * width / previewWidth;
            child.layout(0, (height - scaledChildHeight) / 2,
                    width, (height + scaledChildHeight) / 2);
        }
    }
}

但是cameraPreview泛滥了它的parentView(RelativeLayout): 屏幕应该是(camerapreview到绿色矩形): enter image description here

但在一些弱设备(Android 4.1.2-下面的API 16)中,cameraPreview成为: enter image description here

如您所见,cameraPreview充斥其parentView但是当我捕捉设备的svreen时(通过按住音量降低+电源按钮),图片中没有问题。 如果有人有解决此问题的方法,请指导我。 感谢您的帮助。

0 个答案:

没有答案