LinearLayout 和 CameraSource 不填满屏幕

Posted

技术标签:

【中文标题】LinearLayout 和 CameraSource 不填满屏幕【英文标题】:LinearLayout & CameraSource don't fill screen 【发布时间】:2019-01-28 06:24:24 【问题描述】:

我正在尝试在我的应用程序中构建一个全屏条形码扫描仪,但我无法让仅包含我的 CameraSourceLinearLayout 填满整个屏幕(这篇文章的底部是我的结果截图,带有模拟器中的模拟相机)。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topLayout"
    android:orientation="vertical"
    android:layout_
    android:layout_>

    <com.mattdonders.android.wppcalculator.barcode.CameraSourcePreview
        android:id="@+id/camera_preview"
        android:layout_
        android:layout_
        android:layout_weight="1"/>

</LinearLayout>

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

@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_barcode_scanner);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mPreview = (CameraSourcePreview) findViewById(R.id.camera_preview);

    // Check for the camera permission before accessing the camera.  If the
    // permission is not granted yet, request permission.
    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) 
        createCameraSource();
     else 
        requestCameraPermission();
    

这里是CameraSource类的构造函数:

public CameraSourcePreview(Context context, AttributeSet attrs) 
    super(context, attrs);
    mContext = context;
    mStartRequested = false;
    mSurfaceAvailable = false;

    mSurfaceView = new SurfaceView(context);
    mSurfaceView.getHolder().addCallback(new SurfaceCallback());
    addView(mSurfaceView);

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

【问题讨论】:

如果该类来自 Google 示例,则 SurfaceView 未完全填充。看onLayout() method。 这是 - 很好的捕捉(为什么要修复没有损坏的东西)!他们设置的默认尺寸是 320x240 - 什么尺寸会填满屏幕?我以为这些是标准屏幕比例的像素密度,然后按 DPI 缩放? 320x240 只是默认值。它尝试根据相机的预览大小设置它们,并按比例缩放到View 的可用尺寸。我不知道你所说的“填满屏幕”是什么意思,但layoutWidthlayoutHeightView 的完整尺寸。如果你想让它真正被填充,那么getChildAt(i).layout(0, 0, layoutWidth, layoutHeight);,但这会垂直拉伸它。 检查here。 【参考方案1】:

正如上面我在原始帖子的 cmets 中所提到的,这个问题的答案在于 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);
    

【讨论】:

以上是关于LinearLayout 和 CameraSource 不填满屏幕的主要内容,如果未能解决你的问题,请参考以下文章

LinearLayout 和 CameraSource 不填满屏幕

如何将listview和recyclerview放入linearlayout

如何使用android中的拖放框架从一个LinearLayout切换到另一个LinearLayout

LinearLayout 和 NestedScrollView 问题

LinearLayout 项目和回收站视图之间的稳定距离

LinearLayout的横向和纵向布局问题