посмотрите, ваш XML будет выглядеть так:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.view.SurfaceView
android:id="@+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ImageView
android:id = "@+id/header"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</FrameLayout>
и для java-кода вы должны расширить SurfaceHolder.Callback в своей деятельности, а затем прикрепить камеру к виду поверхности, как это, мой полный код, пожалуйста, будьте осторожны с растровыми изображениями и холстом, который является сложной частью:
public class MainActivity extends Activity implements SurfaceHolder.Callback
{
private Camera camera = null;
private SurfaceView cameraSurfaceView = null;
private SurfaceHolder cameraSurfaceHolder = null;
private boolean previewing = false;
RelativeLayout relativeLayout;
private Button btnCapture = null;
private Button btnsave = null;
private Button btnshare = null;
private boolean isSaved=false;
private boolean isCaptured=false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
relativeLayout=(RelativeLayout) findViewById(R.id.containerImg);
relativeLayout.setDrawingCacheEnabled(true);
cameraSurfaceView = (SurfaceView)
findViewById(R.id.surfaceView1);
// cameraSurfaceView.setLayoutParams(new FrameLayout.LayoutParams(640, 480));
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
// cameraSurfaceHolder.setType(SurfaceHolder.
// SURFACE_TYPE_PUSH_BUFFERS);
btnCapture = (Button)findViewById(R.id.capturebtn);
btnCapture.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
camera.takePicture(cameraShutterCallback,
cameraPictureCallbackRaw,
cameraPictureCallbackJpeg);
isCaptured=true;
}
});
btnsave = (Button)findViewById(R.id.savebtn);
btnsave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FrameLayout frm = (FrameLayout)findViewById(R.id.frameLayout1);
frm.setDrawingCacheEnabled(true);
frm.buildDrawingCache();
Bitmap bitmap = frm.getDrawingCache();
try {
File rootFile=new File(Environment.getExternalStorageDirectory().toString()+"/MYCAMERAOVERLAY");
rootFile.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".png";
File resultingfile = new File(rootFile, fname);
if (resultingfile.exists ()) resultingfile.delete ();
try {
FileOutputStream Fout = new FileOutputStream(resultingfile);
bitmap.compress(CompressFormat.PNG, 100, Fout);
Fout.flush();
Fout.close();
} catch (FileNotFoundException e) {
Log.d("In Saving File", e + "");
}
} catch(IOException e){
Log.d("In Saving File", e + "");
}
isSaved=true;
}
});
btnshare = (Button)findViewById(R.id.sharebtn);
btnshare.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if((isSaved)&&(isCaptured)){
// TODO sharing what ever we saved
// take the path
}
}
});
}
ShutterCallback cameraShutterCallback = new ShutterCallback()
{
@Override
public void onShutter()
{
// TODO Auto-generated method stub
}
};
PictureCallback cameraPictureCallbackRaw = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
// TODO Auto-generated method stub
}
};
PictureCallback cameraPictureCallbackJpeg = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
// TODO Auto-generated method stub
Bitmap cameraBitmap = BitmapFactory.decodeByteArray
(data, 0, data.length);
int wid = cameraBitmap.getWidth();
int hgt = cameraBitmap.getHeight();
// Toast.makeText(getApplicationContext(), wid+""+hgt, Toast.LENGTH_SHORT).show();
Bitmap newImage = Bitmap.createBitmap
(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newImage);
canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
camera.startPreview();
newImage.recycle();
newImage = null;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
}
};
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height)
{
// TODO Auto-generated method stub
if(previewing)
{
camera.stopPreview();
previewing = false;
}
try
{
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(640, 480);
parameters.setPictureSize(640, 480);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
camera.setDisplayOrientation(-90);
}
// parameters.setRotation(90);
camera.setParameters(parameters);
camera.setPreviewDisplay(cameraSurfaceHolder);
camera.startPreview();
previewing = true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
// TODO Auto-generated method stub
try
{
camera = Camera.open();
}
catch(RuntimeException e)
{
Toast.makeText(getApplicationContext(), "Device camera is not working properly, please try after sometime.", Toast.LENGTH_LONG).show();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
// TODO Auto-generated method stub
try{
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}catch(Exception e){
e.printStackTrace();
}
}
}
и я думаю, что теперь я должен дать вам полную XML-часть. так вот оно:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/containerImg"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android1:layout_gravity="bottom"
android:orientation="vertical"
android1:gravity="bottom" >
<FrameLayout
android:id="@+id/frameLayout1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android:gravity="center|top"
>
<SurfaceView
android1:id="@+id/surfaceView1"
android1:layout_width="100dip"
android1:layout_height="150dip"
android1:layout_gravity="center_horizontal" />
<ImageView
android1:id="@+id/imageView1"
android1:layout_width="fill_parent"
android1:layout_height="fill_parent"
android:contentDescription="@string/app_dress_desc"
android:gravity="center|bottom"
android1:scaleType="center"
android1:src="@drawable/dress" />
</FrameLayout>
<GridLayout
android1:id="@+id/gridLayout1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:layout_alignParentBottom="true"
android1:layout_alignParentLeft="true"
android1:columnCount="1"
android1:gravity="center|bottom"
android1:orientation="vertical" >
<ImageButton
android1:id="@+id/capturebtn"
android1:layout_width="60dp"
android1:layout_height="wrap_content"
android1:layout_gravity="left"
android1:src="@drawable/camera"
android:contentDescription="@string/app_icon_camera_desc" />
<ImageButton
android1:id="@+id/savebtn"
android1:layout_width="60dp"
android1:layout_height="wrap_content"
android1:layout_column="0"
android1:layout_gravity="center_horizontal|top"
android1:layout_row="0"
android1:src="@drawable/save"
android:contentDescription="@string/app_icon_save_desc" />
<ImageButton
android1:id="@+id/sharebtn"
android1:layout_width="60dp"
android1:layout_height="wrap_content"
android1:layout_column="0"
android1:layout_gravity="right|top"
android1:layout_row="0"
android1:src="@drawable/share"
android:contentDescription="@string/app_icon_share_desc" />
</GridLayout>
Кнопка «Поделиться» в данный момент не работает, но при захвате она сохраняет весь макет кадра при захвате.
надеюсь, это будет полезно.