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 ?