对于Android Wear设备,可以根据设备方向自动旋转屏幕吗?例如,如果我把手直接放在我面前的空中,掌心向外,表盘将是侧面的,所有文字都难以阅读。
是否有像手持设备上的自动旋转功能,屏幕方向与重力相匹配?
我真的很想在我的应用中启用它。
答案 0 :(得分:0)
到目前为止,我还未能找到满意的答案;如果我在活动定义中(在清单中)设置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));
}