Put a ViewGroup inside a container
I'm trying to create an application开发者_如何学Python where I will have basically a button at the bottom of the screen and a camera preview taking the rest of space. I'm using the CameraPreview example code as a starting point. CameraPreview creates a ViewGroup programmatically and it works fine by its own. What I want now it to put this ViewGroup inside another "view container" that I created in my layout using XML.
So basically, I have a layout and a space where I want to embed the CameraPreview ViewGroup.
Thank you.
I think instead of this:
mPreview = new Preview(this);
setContentView(mPreview);
You would want to do this:
mPreview = new Preview(this);
setContentView(R.layout.YOUR_XML_LAYOUT);
RelativeLayout yourLayoutContainer = (RelativeLayout) findViewById(R.id.YOUR_LAYOUT_CONTAINER_ID);
yourLayoutContainer.addView(mPreview);
This is hugely simplified with error checking and various callbacks removed, also I actually have my camera code in a separate class so that I can use it in different applications, none the less it should move you in the right direction. Although I don't show it here I have the preview and various overlays in one parent container, never had any issues outside of the basic android camera glitches (always has to be in landscape orientation, always has to have the right preview size set etc.)
public class CameraActivity extends Activity {
private SurfaceView cameraSurface = null;
private myCamera camera = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camerasurface);
cameraSurface = (SurfaceView) this.findViewById(R.id.cameraSurface);
camera = new bThereCamera(this,cameraSurface);
}
The XML for the camera activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView android:id="@+id/cameraSurface"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</RelativeLayout>
And a very simplified camera class (the camera class is bound to the lifetime of the activity so there should be no problem with context being passed), but this is just my implementation, there is no reason you can't do this all in line with a single class. All callbacks have been removed except the bare minimum
public class myCamera implements SurfaceHolder.Callback {
private Camera cameraDevice = null;
private SurfaceView cameraSurface = null;
private SurfaceHolder cameraSurfaceHolder = null;
public myCamera(CameraActivity activity, SurfaceView surface)
{
parent = activity;
cameraSurface = surface;
cameraSurfaceHolder = cameraSurface.getHolder();
cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraSurfaceHolder.addCallback(this);
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
Camera.Parameters params = cameraDevice.getParameters();
Camera.Size size = getBestPreviewSize(params,w,h);
if (size != null)
params.setPreviewSize(size.width, size.height);
cameraDevice.setParameters(params);
cameraDevice.startPreview();
}
public void surfaceCreated(SurfaceHolder holder)
{
try {
// Get the device interface
cameraDevice = Camera.open(); // This is different for 2.2 and 2.3+ you will need
// a mediator class if you want to use both ...
cameraDevice.setPreviewDisplay(cameraSurfaceHolder);
} catch (IOException e) { }
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (null == cameraDevice)
return;
cameraDevice.stopPreview();
cameraDevice.release();
cameraDevice = null;
}
public Camera.Size getBestPreviewSize(Camera.Parameters parameters, int w, int h)
{
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPreviewSizes())
{
if (size.width <= w && size.height <= h)
{
if (null == result)
result = size;
else
{
int resultDelta = w - result.width + h - result.height;
int newDelta = w - size.width + h - size.height;
if (newDelta < resultDelta)
result = size;
}
}
}
return result;
}
Also if you want to really dig into the camera download the android camera application version for 2.1 (if you get a later version it will have non compatible stuff) and take a look, they do some interesting things (not shown here) in regards to previews, capture, performance, etc.
精彩评论