android - 检测 Android 方向 : landscape-Left v. landscape-Right

标签 android

如何检测手机的 4 个面中的哪一个面朝上。

我可以检测纵向/横向模式,但如何区分 landscape-turned-on-left-sidelandscape-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/

相关文章:

android - 在同一 Webview 上而不是在浏览器中打开链接

android - 为什么 Android 不允许您在资源名称中使用大写字母?

android - 检测退款的托管应用内购买android IAP 2.0.3

android - 是否可以在通话期间播放音乐以便对方可以听到?安卓

android - 构建错误 :colors. xml:1:1: 错误:序言中不允许内容

java - Android/Eclipse 代码错误

android - CSS:适用于各种移动设备的字体大小、物理按钮高度

带有实时音频流的 Android 背景音频

没有clear()的Android map 更改瓦片覆盖

android - 如何将 registerForActivityResult 与 androidx.fragment.app.Fragment 一起使用?