如何检测的Andr​​oid设备的方向? [英] how to detect orientation of android device?

查看:145
本文介绍了如何检测的Andr​​oid设备的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以简单的检测Android设备当前方向,没有编程监听和处理位置矩阵? 在我的应用我只想知道当前方向 - 垂直或水平 - 的时刻。不过,我不希望听axiometry或其他事件的事件。

Is it possible simple to detect current orientation of android device, without programming a listener and handling the position matrix? In my app I want only to know current orientation - vertical or horizontal - at the moment. However I don't want to listen to events of axiometry or other events.

推荐答案

使用的<一个href="http://developer.android.com/reference/android/view/Display.html#getRotation%28%29"><$c$c>getRotation方法:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

从文档:

返回其自然屏幕方向旋转。返回值可以是 Surface.ROTATION_0 (无旋转), Surface.ROTATION_90 表面.ROTATION_180 Surface.ROTATION_270 。例如,如果一个设备具有自然高大的屏幕,用户已经把它在其一侧进入一个横向,回到这里的值可以是 Surface.ROTATION_90 Surface.ROTATION_270 取决于它转动的方向。的角度是屏幕,这是该装置的物理旋转的相反方向上绘制的图形的转动。例如,如果设备被逆时针旋转90度,以补偿渲染将90度顺时针旋转,因此这里的返回值将是 Surface.ROTATION_90

Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

请注意, getRotation 从Android 2.2的推出。使用 getOrientation 如果你的目标是较旧的设备。

Keep in mind that getRotation was introduced from Android 2.2. Use getOrientation if your target are older devices.

这篇关于如何检测的Andr​​oid设备的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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