Java 如何将按钮添加到相机预览android?

Java 如何将按钮添加到相机预览android?,java,android,xml,android-manifest,Java,Android,Xml,Android Manifest,我正在开发摄影应用程序我的要求是覆盖效果的相机预览,我开发的代码。在这个代码中,我覆盖图像到surfaceview public class OverlayLiveCamera extends Activity { Camera camera; LayoutInflater controlInflater = null; public void onCreate(Bundle savedInstanceState) { supe

我正在开发摄影应用程序我的要求是覆盖效果的相机预览,我开发的代码。在这个代码中,我覆盖图像到surfaceview

public class OverlayLiveCamera extends Activity {
       Camera camera;
       LayoutInflater controlInflater = null;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            Preview mPreview = new Preview(this);
            DrawOnTop mDraw = new DrawOnTop(this);
            setContentView(mPreview);
            addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        }


       class Preview extends SurfaceView implements SurfaceHolder.Callback{

            SurfaceHolder mHolder;



            Preview(Context context) {
                super(context);
                // Install a SurfaceHolder.Callback so we get notified when the
                // underlying surface is created and destroyed.
                mHolder = getHolder();
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            }



            public void surfaceCreated(SurfaceHolder holder) {
                // The Surface has been created, acquire the camera and tell it where
                // to draw.
                camera = Camera.open();
                try {
                    camera.setPreviewDisplay(holder);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            public void surfaceDestroyed(SurfaceHolder holder) {
                // Surface will be destroyed when we return, so stop the preview.
                // Because the CameraDevice object is not a shared resource, it's very
                // important to release it when the activity is paused.
                camera.stopPreview();
                camera = null;
            }

            public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
                // Now that the size is known, set up the camera parameters and begin
                // the preview.
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(w, h);
                parameters=camera.getParameters();
                camera.setParameters(parameters);
                camera.startPreview();
            }
       }
        class DrawOnTop extends View {

            public DrawOnTop(Context context) {


                super(context);

                // TODO Auto-generated constructor stub
            }

            @Override
            protected void onDraw(Canvas canvas) {
                // TODO Auto-generated method stub
                Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.water);
                Paint paint = new Paint();
                paint.setAlpha(100);
                canvas.drawBitmap(mBitmap, 0, 0, paint); 


                super.onDraw(canvas);
            }

        }
我的xml代码

<FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <SurfaceView android:id="@+id/surfaceView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></SurfaceView>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>

</FrameLayout>

因为它将出现查看和覆盖图像,但它不会显示按钮,而且当我单击按钮时,覆盖视图捕获并保存到sd卡中


请帮助我。

您可以使用下面的代码添加按钮

 <FrameLayout android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="2"
  android:padding="5dip"  
  >
  <com.commonlib.controls.CameraPreview
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:layout_gravity="center"
  android:id="@+id/surface_camera"
  />
  <Button android:id="@+id/target"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="surfacebuttonview"
    >
    </Button>
</FrameLayout>

U可以使用下面的代码添加按钮

 <FrameLayout android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="2"
  android:padding="5dip"  
  >
  <com.commonlib.controls.CameraPreview
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:layout_gravity="center"
  android:id="@+id/surface_camera"
  />
  <Button android:id="@+id/target"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="surfacebuttonview"
    >
    </Button>
</FrameLayout>