Android在onlayout方法中调整另一个视图的大小

时间:2011-06-08 12:08:51

标签: android layout camera surfaceview viewgroup

我遇到了这个问题: 我有一个自定义视图组,用于处理我在其上渲染相机预览的surfaceview。 在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();
        }
    }
}

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

  • 在活动启动时正确调整了相机表面视图 IS
  • FrameImage 调整大小。但是,我确信(感谢日志)_viewToUpdate不为null。
  • 如果,在第二个之后,使用onClick事件,我在自定义ViewGroup上调用requestLayout()来重新计算surfaceView和FrameImage的大小, IT WILL 然后正确调整FrameImage的大小。< / LI>

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

1 个答案:

答案 0 :(得分:1)

你试过了吗?

maxl += 40;

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