【问题标题】:View doesn't fill the whole screen视图没有填满整个屏幕
【发布时间】:2015-09-11 14:09:07
【问题描述】:

我在https://github.com/googlesamples/android-vision/tree/master/visionSamples/multi-tracker 中拥有与此示例相同的所有内容,除了我的活动布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:weightSum="100"
    android:orientation="horizontal">

    <be.citylife.communitypurchaseapp.view.camera.CameraSourcePreview
        android:id="@+id/preview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60">

        <be.citylife.communitypurchaseapp.view.camera.GraphicOverlay
            android:id="@+id/overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </be.citylife.communitypurchaseapp.view.camera.CameraSourcePreview>

    <FrameLayout
        android:id="@+id/sideContainer"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40"/>

</LinearLayout>

我的平板电脑处于横向状态,我希望 cameraPreviewSource 始终处于左侧并以高度填充整个屏幕,然后在右侧我有一个片段可以填充其余部分。

除了我的 previewsource 没有填满整个高度之外,此布局有效。它上面有一个黑色的横幅。甚至我的宽度实际上比我想要的要小,您可以在屏幕截图中看到:

http://i61.tinypic.com/vctmw0.png

我在 onLayout 函数中使用了具有宽度和高度的 CameraSourcePreview,但它没有帮助。我在预览中知道它确实将屏幕填充到屏幕底部,但在平板电脑上却不是。

lp.

有人知道如何解决这个问题吗?

编辑:

我认为这与此有关:

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

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

        final int layoutWidth = right - left;
        final int layoutHeight = bottom - top;

        // Computes height and width for potentially doing fit width.
        int childWidth = layoutWidth;
        int childHeight = (int)(((float) layoutWidth / (float) width) * height);

        // If height is too tall using fit width, does fit height instead.
        if (childHeight > layoutHeight) {
            childHeight = layoutHeight;
            childWidth = (int)(((float) layoutHeight / (float) height) * width);
        }

        for (int i = 0; i < getChildCount(); ++i) {
            getChildAt(i).layout(0, 0, childWidth, childHeight);
        }

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

这是CameraSourcePreview 的onlayout 方法。

【问题讨论】:

  • 我有同样的问题,当相机打开时,部分视图是黑色的。你找到解决办法了吗?

标签: android


【解决方案1】:

从 CameraSourcePreview 中注释或删除以下行,应该没问题。我和你有同样的问题,现在已经解决了。

if (childHeight > layoutHeight) {
    childHeight = layoutHeight;
    childWidth = (int)(((float) layoutHeight / (float) height) * width);
}

【讨论】:

  • 为我工作!不确定这样做是否有任何影响,但现在会使用
  • 相机高度已定型。有解决办法吗?
【解决方案2】:

应该将其置于全屏模式 :D 有许多其他模式可供您选择。如果这不起作用,请删除自动生成的

    @Override
public boolean onCreateOptionsMenu(Menu menu) 
{

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}




/** gets called when a Menu.onClick happens
 * 
 * @param item the ID of the clicked Item
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        //TODO
    }
    return super.onOptionsItemSelected(item);
}
}

【讨论】:

  • 但这无助于解决我的问题。问题出在设备本身。
【解决方案3】:

可能与相机的纵横比以及它如何绘制到其容器中有关。

如果您使用的相机预览保持纵横比并且适合预览到容器,那么您肯定会得到黑条。这是因为大多数相机的传感器生成的图像设计为适合相对于 1920x1080 像素(或 16:9 纵横比框)的空间。

您需要的是隐藏侧面的额外空间并根据高度填充预览。也就是说,如果您不介意在预览时对用户隐藏某些图像。可能无法直接使用您正在使用的视图执行此操作,但如果您将对象放入另一个布局容器中,它应该相对简单。

希望这会有所帮助!

【讨论】:

  • 所以我需要用xml中的另一个布局包装我的布局来完成这个?或者你是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
相关资源
最近更新 更多