【问题标题】:Wearables Rotate Screen Based on Orientation可穿戴设备根据方向旋转屏幕
【发布时间】:2015-01-08 15:05:16
【问题描述】:

对于 Android Wear 设备,是否可以根据设备方向启用屏幕自动旋转?例如,如果我将手笔直地举在面前的空中,掌心向外,表盘会侧向一边,所有文字都将难以阅读。

是否有像手持安卓设备那样的自动旋转功能,屏幕方向将匹配重力?

我真的很想在我的应用中启用此功能。

【问题讨论】:

    标签: android rotation orientation autorotate


    【解决方案1】:

    到目前为止,我还没有找到满意的答案;如果我在活动定义(清单中)中设置android:screenOrientation="fullSensor",这将被完全忽略。如果我更改主题,它们似乎也没有效果。

    也就是说,如果需要,可以手动更改方向。我个人不喜欢这个解决方案,所以如果有人有更好的解决方案,请贡献一个。 (旁注:我意识到 Wear 应用程序不自动定向是有意义的,但我的客户已要求这样做,因此需要以一种或另一种方式实现。)

    此代码并不完美,但如果您需要,它应该可以帮助您入门:

    @Override
    protected void onResume() {
        super.onResume();
        // .. other code ..
        startRotationListening();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // .. other code ..
        stopRotationListening();
    }
    
    protected void startRotationListening() {
        try {
            SensorManager sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
            sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SENSOR_DELAY);
            listeningToRotation = true;
        } catch (Exception e) {
            Log.e(TAG, "Rotation hardware? What hardware?", e);
        }
    }
    
    protected void stopRotationListening() {
        if (listeningToRotation) {
            try {
                SensorManager sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
                sensorManager.unregisterListener(this);
                listeningToRotation = false;
            } catch (Exception e) {
                Log.e(TAG, "Rotation hardware? What hardware?", e);
            }
        }
    }
    
    @Override
    public void onSensorChanged(SensorEvent event) {
        //if (event.sensor == rotationSensor) {
            if (event.values.length > 4) {
                float[] truncatedRotationVectors = new float[4];
                System.arraycopy(event.values, 0, truncatedRotationVectors, 0, 4);
                onRotationChange(truncatedRotationVectors);
            }
        //}
    }
    
    protected void onRotationChange(float[] vectors) {
        float[] rotationMatrix = new float[9], adjustedMatrix = new float[9], orientation = new float[3];
        SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
        SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, adjustedMatrix);
        SensorManager.getOrientation(adjustedMatrix, orientation);
    
        float pitch = orientation[1] * RADS_TO_DEGS, roll = orientation[2] * RADS_TO_DEGS;
    
        String strOrientation = null;
        int screenOrientation = -1;
        if (pitch <= -80 && pitch >= -100) {
            // Too flat (face up), ignore orientation
            strOrientation = "FLAT_UP";
        } else if (pitch >= 80 && pitch <= 100) {
            // Too flat (face down), ignore orientation
            strOrientation = "FLAT_DN";
        }
    
        if (strOrientation == null) {
            if (roll >= -10 && roll <= 10) {
                screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
                strOrientation = "UPRIGHT";
            } else if (roll <= -80 && roll >= -110) {
                screenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
                strOrientation = "ONRIGHT";
            } else if (roll >= 170 || roll <= -170) {
                screenOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
                strOrientation = "TOPSIDE";
            } else if (roll >= 80 && roll <= 100) {
                screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                strOrientation = " ONLEFT";
            } else {
                strOrientation = "  ???  ";
            }
        }
    
        if (screenOrientation != -1) {
            setRequestedOrientation(screenOrientation);
        }
        Log.d(TAG, String.format("%s Pitch: %f, roll: %f", strOrientation, pitch, roll));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多