我已经想出了如何完成这项工作。这是一个迂回的方法,但它是:设置一个传感器管理器和一个定位传感器
public class ActivityRecordVideo extends ActivityBase implements SensorEventListener
{
private SensorManager mSensorManager;
private Sensor mOrientation;
@Override
public void onCreate(Bundle savedInstanceState)
{
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if(camId != -1 && currentDisplayRotation != rotation)
{
if(!isRecording)
setCameraOrientation(camId, cameraRecorder);
}
}
我还在 onResume() 中设置了侦听器,并在 onPause() 中删除了它们。
这允许相机方向与其他所有东西一起翻转(例如,当处于反向横向时,所有视图与相机预览一起处于反向横向)
还决定展示 setCameraOrientation 代码,它是 android 开发者代码的简化版
private void setCameraOrientation(int camId, Camera camera)
{
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(camId, info);
currentDisplayRotation = getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch(currentDisplayRotation)
{
case Surface.ROTATION_0: degrees = 0; break;
case Surface.ROTATION_90: degrees = 90; break;
case Surface.ROTATION_180: degrees = 180; break;
case Surface.ROTATION_270: degrees = 270; break;
}
int result;
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; //compensate for mirror effect
if(Build.VERSION.SDK_INT < 14)
camera.stopPreview();
camera.setDisplayOrientation(result);
if(Build.VERSION.SDK_INT < 14)
camera.startPreview();
}
希望这对其他人有帮助