1

我遇到了这个问题:我有一个自定义视图组来处理我渲染相机预览的表面视图。在 onLayout 方法上,我调整了这个 surfaceView 的大小以正确显示相机。

此自定义视图组位于我要在相机预览上方渲染的图像后面的相对布局中。

<RelativeLayout android:id="@+id/LinearLayout1"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="match_parent">
    <LinearLayout android:orientation="vertical" android:id="@+id/linearLayout2" android:layout_alignParentTop="true" android:layout_height="match_parent" android:layout_width="match_parent">
        <com.ltu.sdk.LTUCameraView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/CameraView"></com.ltu.sdk.LTUCameraView>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayout4"
        android:orientation="vertical"
        android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="match_parent">
        <ImageView android:src="@drawable/frame" android:id="@+id/FrameImage" android:scaleType="fitXY" android:layout_width="match_parent" android:layout_height="match_parent"></ImageView>
    </LinearLayout>
</RelativeLayout>

现在,我想在调整相机 surfaceView 的大小时调整 FrameImage 的大小,以便图像始终恰好位于相机的顶部。这发生在 onLayout 方法中。我在调整 surfaceView 大小的同时调整 ImageView 的大小(在属性 _viewToUpdate 中):

protected void onLayout(boolean changed, int l, int t, int r, int b) {
        /* ... LOTS OF CALCULATIONS ... */

        // 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);
            if (_viewToUpdate != null)
                _viewToUpdate.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);
            if (_viewToUpdate != null)
                _viewToUpdate.layout(0, (height - scaledChildHeight) / 2, width, (height + scaledChildHeight) / 2);
        }
        if (_viewToUpdate != null) {
            Log.e("Salomon", "_viewToUpdate.requestLayout();");
            _viewToUpdate.invalidate();
            _viewToUpdate.forceLayout();
            _viewToUpdate.requestLayout();
        }
    }
}

现在这有一个令人惊讶的行为:

  • 相机表面视图在活动启动时正确调整大小
  • FrameImage 未调整大小。但是,我确信(感谢日志)_viewToUpdate 不为空。
  • 如果在第二秒之后,使用 onClick 事件,我在自定义 ViewGroup 上调用 requestLayout() 以重新计算 surfaceView 和 FrameImage 的大小,然后它将正确调整 FrameImage 的大小。

我需要根据相机大小在活动启动时调整此 FrameImage 的大小。我不明白为什么它不与我当前的代码......我该怎么办?

4

1 回答 1

1

你有没有尝试过:

_viewToUpdate.getHandler().post(new Runnable() {
                @Override
                public void run() {
                    _viewToUpdate.requestLayout();
                }
});

我以这种方式解决了类似的问题。

于 2016-04-22T19:55:10.007 回答