检测机器人的方向:景观左v景观权 [英] Detect Android orientation: landscape-Left v. landscape-Right

查看:137
本文介绍了检测机器人的方向:景观左v景观权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测这些手机的四边之一了起来。

How can I detect which one of 4 sides of the phone is up.

我可以检测纵向/横向模式,但我怎么告诉景观出身的上左侧景观出身的上右侧

I can detect portrait/landscape mode, but how do I tell landscape-turned-on-left-side from landscape-turned-on-right-side?

基本上我想,当用户打开手机,使一个很好的过渡动画。你知道,像在iPhone的Safari浏览器:迅速400ms的循环中的previous布局新

Basically I want to make a nice transition animation when user turns phone. You know, like in iPhone's Safari: a swift 400ms rotation from the previous layout to the new.

推荐答案

使用的OrientationEventListener:

Use an OrientationEventListener:

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) 
{ 
 @Override
 public void onOrientationChanged(int orientation) 
 {
  mDeviceOrientation = orientation;
 }
};

if(mOrientationEventListener.canDetectOrientation())
{
 mOrientationEventListener.enable();
}

mDeviceOrientation应该再是一个整数告诉你你的设备旋转的角度,如果你做一些聪明的四舍五入,你应该能够看到这四个方位它是:

mDeviceOrientation should then be an integer telling you the angle your device is rotated to, if you do some clever rounding you should be able to see which of the four orientations it is in:

// 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;
}

享受!

这篇关于检测机器人的方向:景观左v景观权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆