【问题标题】:What gets called with sensorLandscapesensorLandscape 叫什么
【发布时间】:2013-02-01 17:45:07
【问题描述】:

我有一个只想在横向模式下运行的应用程序(例如横向或反向横向),所以我在清单中使用 sensorLandscape。

我的问题在这里: 我也有一个录像机工作(例如使用相机)。现在我在准备相机时设置了显示方向,但问题是如果我改变方向,改变就会保持(预览是颠倒的)。使用传感器景观时,我如何知道 UI 何时更改。我曾尝试使用 onConfigurationCanged() 但不幸的是,我需要让应用程序在预蜂窝设备(主要是姜饼)和后蜂窝设备(果冻豆)上运行。

因为为了正确调用 onConfigurationChanged,我需要将目标 api 设置为 8,但传感器景观仅适用于 9+。我需要将其设置为 8,因为在较旧的 api 中,您需要将 screenSize 添加到“configChanges”,否则将不会被调用。所以这些调用之间有很多不兼容的地方。

现在背景已经完成,你们知道我已经尝试过什么了。我的问题是 我怎样才能知道我的方向何时发生变化(回调或其他),以便我可以更改我的相机显示方向?

提前谢谢你。

【问题讨论】:

    标签: android configuration android-camera android-sensors android-mediarecorder


    【解决方案1】:

    我已经想出了如何完成这项工作。这是一个回合,但这里是: 设置传感器管理器和定位传感器

    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();
    }
    

    希望这对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 2012-08-17
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多