3

我是android新手我在这里从头开始创建了一个相机应用程序我在运行时创建了surfaceview我想在它上面创建一个按钮我该怎么做???

这是我的代码相机显示完美希望在其上方添加一个按钮以捕获图像

package com.example.urvey.tool;
import android.os.Bundle;
import java.lang.*;
import android.view.MenuItem;
import android.widget.*;
import android.support.v4.app.NavUtils;
import android.text.Layout;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.content.Intent;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.*;



@SuppressLint({ "SdCardPath", "NewApi" }) public class Camera_screen extends Activity {
    //AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
       Preview P = new Preview(this);
    setContentView(P);

        Button myButton = new Button(this);
        myButton.setText("Press Me");
System.out.println("halyu");
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.cam_out);
        layout.addView(myButton);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_camera, menu);
        return true;
    }



    @SuppressLint("SdCardPath") class Preview extends SurfaceView implements SurfaceHolder.Callback {

        SurfaceHolder mHolder;
        Camera mCamera;



        public Preview(Context context) {

            super(context);

            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = this.getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            System.out.println("constructor");
            }


        @SuppressLint({ "SdCardPath", "SdCardPath" }) public void surfaceCreated(SurfaceHolder holder) {
            // The Surface has been created, acquire the camera and tell it where
            // to draw.

            try {

                 mCamera = Camera.open();
               mCamera.setPreviewDisplay(holder);

            } catch (IOException exception) {
                mCamera.release();
                mCamera = null;



                // TODO: add more exception handling logic here
            }
        }

        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.
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }

        public void previewCamera()
        {        
            try 
            {           
                mCamera.setPreviewDisplay(mHolder);          
                mCamera.startPreview();
            }
            catch(Exception e)
            {

            }
        }


        @SuppressLint("NewApi") public Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters){
            Camera.Size bestSize = null;
            List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes();

            bestSize = sizeList.get(0);

            for(int i = 1; i < sizeList.size(); i++){
             if((sizeList.get(i).width * sizeList.get(i).height) >
               (bestSize.width * bestSize.height)){
              bestSize = sizeList.get(i);
             }
            }

            return bestSize;
           }



        @SuppressLint("NewApi") 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 = mCamera.getParameters();
                 Camera.Size myBestSize = getBestPreviewSize(3, 4, parameters);
              /* List<Size> sizes = parameters.getSupportedPreviewSizes();
                Size   size = sizes.get(0);*/
                if(myBestSize != null){
                 mCamera.setDisplayOrientation(90);
                    parameters.setPreviewSize(myBestSize.width, myBestSize.height);
                    mCamera.setParameters(parameters);
                    mCamera.startPreview();
              }






                mCamera.startPreview();



        }


    }

}
4

1 回答 1

2

试试这个作为你的替代品onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    // Preview P = new Preview(this);
    // setContentView(P);

    Button myButton = new Button(this);
    myButton.setText("Press Me");
    myButton.setOnClickListener(this);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.cam_out);
    layout.addView(myButton);

}

public void onClick(View v) {
    Log.i("onClick", "myButton");
    // Process the Image, etc
}
于 2012-10-24T20:08:21.330 回答