【发布时间】: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,但它没有帮助。我在预览中知道它确实将屏幕填充到屏幕底部,但在平板电脑上却不是。
有人知道如何解决这个问题吗?
编辑:
我认为这与此有关:
@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