LinearLayout和CameraSource无法填满屏幕

时间:2018-08-22 02:55:16

标签: android android-layout android-camera

我正在尝试在我的应用中构建全屏条形码扫描仪,但我无法获得仅包含subprocess.check_output(['git', 'shortlog'])的{​​{1}}来填满整个屏幕(本文的底部是的屏幕截图)我在模拟器中使用仿真相机拍摄的结果)。

这是我的布局:

subprocess.check_output(['git', 'log'])

这是我的BarcodeScanner活动中的onCreate方法:

LinearLayout

这是CameraSource类的构造函数:

CameraSource

未完全填充视图的屏幕截图。 Screenshot

1 个答案:

答案 0 :(得分:0)

如上所述,在我的原始帖子的评论中,对此的答案在于CameraSourcePreview类的onLayout方法中。在实施Github问题中提到的一些解决方案的基础上,this answer对我来说是最有效的,并且我已经复制/粘贴了下面的代码,因此该问题的答案不仅仅是一个链接。

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int previewWidth = 320;
    int previewHeight = 240;
    if (mCameraSource != null) {
        Size size = mCameraSource.getPreviewSize();
        if (size != null) {
            previewWidth = size.getWidth();
            previewHeight = size.getHeight();
        }
    }

    // Swap width and height sizes when in portrait, since it will be rotated 90 degrees
    if (isPortraitMode()) {
        int tmp = previewWidth;
        previewWidth = previewHeight;
        previewHeight = tmp;
    }

    final int viewWidth = right - left;
    final int viewHeight = bottom - top;

    int childWidth;
    int childHeight;
    int childXOffset = 0;
    int childYOffset = 0;
    float widthRatio = (float) viewWidth / (float) previewWidth;
    float heightRatio = (float) viewHeight / (float) previewHeight;

    // To fill the view with the camera preview, while also preserving the correct aspect ratio,
    // it is usually necessary to slightly oversize the child and to crop off portions along one
    // of the dimensions.  We scale up based on the dimension requiring the most correction, and
    // compute a crop offset for the other dimension.
    if (widthRatio > heightRatio) {
        childWidth = viewWidth;
        childHeight = (int) ((float) previewHeight * widthRatio);
        childYOffset = (childHeight - viewHeight) / 2;
    } else {
        childWidth = (int) ((float) previewWidth * heightRatio);
        childHeight = viewHeight;
        childXOffset = (childWidth - viewWidth) / 2;
    }

    for (int i = 0; i < getChildCount(); ++i) {
        // One dimension will be cropped.  We shift child over or up by this offset and adjust
        // the size to maintain the proper aspect ratio.
        getChildAt(i).layout(
                -1 * childXOffset, -1 * childYOffset,
                childWidth - childXOffset, childHeight - childYOffset);
    }

    try {
        startIfReady();
    } catch (IOException e) {
        Log.e(TAG, "Could not start camera source.", e);
    }
}