Wearables Rotate Screen based on orientation

For Android Wear devices, can you enable automatic screen rotation based on device orientation? For example, if I hold my hand straight in the air in front of me, palm, then the face will be sideways and the entire text will be difficult to read.

Is there an auto-rotate feature like on handheld Android devices where the screen orientation will match gravity?

I would love to include this in my application.

+3


source to share


1 answer


So far, I have not been able to find a satisfactory answer to this; If I set android:screenOrientation="fullSensor"

in the activity definition (in the manifest), this is completely ignored. If I change themes, they don't seem to work either.

However, you can manually change the orientation if necessary. I personally don't like this solution, so if anyone has a better one, please do so. (Side note: I understand that it makes sense for Wear apps not to auto-navigate, but this request was requested by my client, so it must be implemented one way or another.)



This code isn't perfect, but it should get started if you need it:

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

      

0


source







All Articles