1

I got this problem : I have a custom viewgroup that handles a surfaceview on which I render the camera preview. On the onLayout method, I resize this surfaceView to correctly display the camera.

This custom viewgroup is in a relative layout behind an image that I want to render above the camera preview.

<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>

Now I want to resize the FrameImage when I resize the camera surfaceView so the image will always be exactly on top of the camera. This happens in the onLayout method. I resize the ImageView (in the property _viewToUpdate) at the same time when I resize the surfaceView :

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();
        }
    }
}

Now this has a surprising behavior :

  • The camera surface view IS correctly resized on activity launch
  • The FrameImage IS NOT resized. However, I am sure (thanks to the log) that _viewToUpdate is not null.
  • If, on second later, using an onClick event, I call requestLayout() on the custom ViewGroup to recalculate the size of the surfaceView AND the FrameImage, IT WILL then correctly resize the FrameImage.

I need this FrameImage to be resized on activity launch according to the camera size. I don't understand why it doesn't with my current code... What should I do ?

timelzayus
  • 73
  • 2
  • 4
  • I have the same problem. didn't find a sollution normal solution though. Did found a work around. By setting a onClick event in the layout on a (invisible)view/button/layout and call the performClick of that view. Setting the layout params works then because Onlayout is being called again – EvertvdBraak Aug 09 '12 at 10:12
  • @Charx Have you found a normal solution since? – ilomambo May 22 '13 at 10:21

1 Answers1

0

Have you tried:

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

I've solved a similar problem this way.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
tom
  • 19
  • 2