我是新手Android开发者,从零开始创建了一个相机app。现在我创建了一个运行时的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();
}
}
}
myButton
是你要询问的按钮吗?如果是,它是否出现错误或没有显示? - jnthnjnssetContentView()
有关,我假设您添加按钮的RelativeLayout layout
在activity_camera
布局中,但您将setContentView()
更改为Preview
。请注释掉该行并运行应用程序以查看是否显示。此外,如果按钮是恒定的,则不一定需要在程序中构建它。 - jnthnjns