对于Android Wear设备,可以根据设备方向启用屏幕自动旋转吗?例如,如果我把手掌面朝外伸直在空中,手表的表面将是侧着的,所有文本都很难阅读。
是否有像手持式Android设备上一样的自动旋转功能,使屏幕方向与重力匹配?
我真的很想在我的应用程序中启用这个功能。
是否有像手持式Android设备上一样的自动旋转功能,使屏幕方向与重力匹配?
我真的很想在我的应用程序中启用这个功能。
到目前为止,我还没有找到一个令人满意的答案;如果我在活动定义中(在清单文件中)设置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));
}