How should I add elements to a custom camera andro

2019-08-09 02:23发布

问题:

my android app uses a custom camera. I just have a framelayout (full screen) in my xml layout file to which i programmatically add a surfaceview that contains the camera preview.

Now I would like to add some buttons to my frame layout. I know that i could just add those buttons all programmatically, but is there a nicer way to do it. I mean, could I create something like a mask / layout that contains some buttons and that I then programmatically add to my frame layout?

How do professional app makers do that? I just want to know all this so that I can keep a good design.

Thank you so much!

回答1:

I think that articles will be helpful for you :

http://android-er.blogspot.com/2010/12/add-overlay-on-camera-preview.html

How to create a mask for a transparent overlay?

Adding bitmap mask to video on Android?



回答2:

Create your layout xml file. From the information you have given, the base will be a FrameLayout. Place buttons and other widgets wherever you need to. As an example, say you place 3 buttons inside the FrameLayout: b1, b2 and b3.

When the SurfaceView is added, the buttons will not be visible. Do this to show them on top of the SurfaceView:

// Initially

frameLayout = (FrameLayout) findViewById(R.id.framelayout_id_from_xml_file);

b1 = (Button) findViewById(R.id.button_id_from_xml_file_1);

b2 = (Button) findViewById(R.id.button_id_from_xml_file_2);

b3 = (Button) findViewById(R.id.button_id_from_xml_file_3);

....
....
....

// create a SurfaceView and add it to the framelayout

frameLayout.add(sufaceView);

// now, remove the buttons and add them again

frameLayout.removeView(b1);
frameLayout.addView(b1);

frameLayout.removeView(b2);
frameLayout.addView(b2);

frameLayout.removeView(b3);
frameLayout.addView(b3);