I am creating a Camera application where I want to restrict the camera preview to be centered and around 60% of total size of the phone screen.
- One way is to have the FrameLayout of Preview with padding so that the usable screen size is restricted , but I also want to show borders (around the corners) inside the restricted area which is not possible in this options.Also it not possible to show padding in percentage so that I can restrict user to 60% of screen size.
- Other way is to have a image having a transparent center rectangular area and having border in inner rectangle corners . But I somehow need to restrict the FrameLayout of preview inside this rectangle which I am not able to do . Any suggestions on this option ?
Please also let me know if there are any other options .
Thanks.
Unless I don’t fully understand your issues with using
android:paddingto provide space around your layout, you should be able to useandroid:layout_margininstead to accomplish the same goal but overcome the problems you mentioned. Adding padding creates space between the border of the View and its content. However, adding margin creates space between the border of the View and its parent, so the content still fills the View itself to the edges. However, you still can’t define your view spacing in terms of percentage direct from XML…you would need to define the margin as static or apply theLayoutParamsin Java code where you could calculate the required margin based on the current screen size.Another option is to take advantage of the
android:layout_weightproperty inside of a nestedLinearLayout. The calculated weight sum could give you the 60% you’re looking for directly in XML. Something like this:Where the
SurfaceViewis the location of your Camera Preview, now centered with 20% of the screen on all sides. If you wanted to place things over or under this preview you would obviously want to place this entire block into aRelativeLayoutorFrameLayoutalong with other components.HTH!