我有以下java代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import com.testing.CameraOperations;
public class MainActivity extends Activity {
private SurfaceView surface;
private CameraOperations camera;
private SoundOperations sound;
private static boolean woken = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("unused")
public void test(View v){
if(!woken){
surface = (SurfaceView)findViewById(R.id.cameraView);
SurfaceHolder holder = surface.getHolder();
surface.setCamera(camera.toggleFaceCamera());
Animation out = AnimationUtils.makeOutAnimation(this, true);
v.startAnimation(out);
v.setVisibility(View.INVISIBLE);
woken = true;
}
}
}
另一个班级 cameraOperations :
import android.hardware.Camera;
public class CameraOperations{
private Camera cam = null;
private Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
private static int camNumber = Camera.getNumberOfCameras();
public Camera toggleFaceCamera(){
if(cam == null){
for(int counter = 0; counter < camNumber; counter++){
Camera.getCameraInfo(counter, cameraInfo);
if(cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
try{
cam = Camera.open(counter);
return cam;
}catch(RuntimeException e){return null;}
}
}
}
return null;
}
}
按钮onclick运行“test”功能,动画效果很好。 我已经阅读了一些教程和对此的参考但是我无法成功地向用户预览相机。如果有人能够让我清楚地理解并通过编码之间的关系表面,支架和相机预览我会非常感激!