最重要的是,我有一个自定义相机类,预览效果很好,只需轻轻一点就可以点击ClickListener。但是,我需要屏幕上的按钮来选择闪光灯选项和更换相机等等,而且它们显然会有自己的自定义图像。现在这看起来应该很简单,但无论我做什么,我似乎无法让按钮出现在我的预览中。这是相机和xml。 公共类TakePictureActivity扩展Activity { 私人预览mCameraView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
mCameraView = new Preview(this);
setContentView(mCameraView);
}
class Preview extends SurfaceView implements SurfaceHolder.Callback, Camera.PictureCallback {
private SurfaceHolder mHolder;
private Camera mCamera;
private RawCallback mRawCallback;
public Preview(Context context){
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mRawCallback = new RawCallback();
/*
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(mRawCallback, mRawCallback, null, Preview.this);
}
});
*/
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(width, height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
configure(mCamera);
try{
mCamera.setPreviewDisplay(holder);
}catch(IOException e){
closeCamera();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
closeCamera();
}
private void configure(Camera camera){
Camera.Parameters parameters = camera.getParameters();
List<Integer> formats = parameters.getSupportedPictureFormats();
if(formats.contains(PixelFormat.RGB_565))
parameters.setPictureFormat((PixelFormat.RGB_565));
else
parameters.setPictureFormat(PixelFormat.JPEG);
List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
Camera.Size size = sizes.get(sizes.size() - 1);
parameters.setPreviewSize(size.width, size.height);
List<String> flashModes = parameters.getSupportedFlashModes();
if(flashModes.size() > 1)
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
List<String> sceneModes = parameters.getSupportedSceneModes();
if(sceneModes.contains(Camera.Parameters.SCENE_MODE_ACTION))
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_ACTION);
else
parameters.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
camera.setDisplayOrientation(90);
else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
camera.setDisplayOrientation(180);
camera.setParameters(parameters);
}
private void closeCamera(){
if(mCamera != null){
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
mCamera.startPreview();
}
简单地为ImageButton实例化onClickListener也会崩溃我的应用程序。哈哈,我只是想让它显示出来。一步一步。
谢谢!
修改
这是我的XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".TakePictureActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton4"
android:layout_gravity="center_horizontal|bottom"
android:src="@drawable/ic_menu_camera"/>