Android页面方向、角度及旋转小结
手上的项目原本设计之初是基于手机竖屏的,但由于近期某些订制版本需要适配横屏和默认横屏设备,因此对页面的朝向和角度获取要进行一些研究。其中也踩了不少坑,这里拿出来给大家乐呵乐呵。
从传感器角度讲起
通常,我们会使用OrientationEventListener来获取传感器角度,其一般用法如下:
new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
@Override
public void onOrientationChanged(int orientation) {
if (orientation > 350 || orientation < 10) {
//0度
} else if (orientation > 80 && orientation < 100) {
//90度
} else if (orientation > 170 && orientation < 190) {
//180度
} else if (orientation > 260 && orientation < 280) {
//270度
}
}
}.