如何检测手机的 4 个面中的哪一个面朝上。
我可以检测纵向/横向模式,但如何区分 landscape-turned-on-left-side 和 landscape-turned-on-right-side?
基本上我想在用户转动手机时制作一个漂亮的过渡动画。您知道,就像在 iPhone 的 Safari 中一样:从以前的布局到新布局的快速 400 毫秒旋转。
最佳答案
使用 OrientationEventListener:
mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
{
@Override
public void onOrientationChanged(int orientation)
{
mDeviceOrientation = orientation;
}
};
if(mOrientationEventListener.canDetectOrientation())
{
mOrientationEventListener.enable();
}
mDeviceOrientation 应该是一个整数,告诉您设备旋转的角度,如果您进行一些巧妙的舍入,您应该能够看到它处于四个方向中的哪一个:
// Divide by 90 into an int to round, then multiply out to one of 5 positions, either 0,90,180,270,360.
int orientation = 90*Math.round(mDeviceOrientation / 90);
// Convert 360 to 0
if(orientation == 360)
{
orientation = 0;
}
尽情享受吧!
关于android - 检测 Android 方向 : landscape-Left v. landscape-Right,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727800/