【问题标题】:Android Resizing another view in an onlayout methodAndroid在onlayout方法中调整另一个视图的大小
【发布时间】:2011-06-08 12:08:51
【问题描述】:

我遇到了这个问题: 我有一个自定义视图组来处理我渲染相机预览的表面视图。 在 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 不为空。
  • 如果在第二秒之后,我使用 onClick 事件调用自定义 ViewGroup 上的 requestLayout() 以重新计算 surfaceView 和 FrameImage 的大小,它会然后正确调整 FrameImage 的大小。

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

【问题讨论】:

  • 我也有同样的问题。虽然没有找到解决方案正常的解决方案。确实找到了解决方法。通过在(不可见)视图/按钮/布局上的布局中设置 onClick 事件并调用该视图的 performClick。然后设置布局参数就可以了,因为 Onlayout 再次被调用
  • @Charx 从那以后你找到正常的解决方案了吗?

标签: android layout camera surfaceview viewgroup


【解决方案1】:

你试过了吗:

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

我用这种方法解决了一个类似的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多