5. 实时判断屏幕旋转的每一个角度
上面说的各种屏幕旋转角度的判断至多只能判断 0,90,180,270 四个角度,如果希望实时获取每一个角度的变化,则可以通过OrientationEventListener 来实现。
使用方法:
(1)创建一个类继承OrientationEventListener
public class MyOrientationDetector extends OrientationEventListener{
public MyOrientationDetector( Context context ) {
super(context );
}
@Override
public void onOrientationChanged(int orientation) {
Log.i("MyOrientationDetector ","onOrientationChanged:"+orientation);
}
}
(2)开启和关闭监听
可以在 activity 中创建MyOrientationDetector 类的对象,注意,监听的开启的关闭,是由该类的父类的 enable() 和 disable() 方法实现的。
因此,可以在activity的 onResume() 中调用MyOrientationDetector 对象的 enable方法,在 onPause() 中调用MyOrientationDetector 对象的 disable方法来完车功能。
(3)监测指定的屏幕旋转角度
MyOrientationDetector类的onOrientationChanged 参数orientation是一个从0~359的变量,如果只希望处理四个方向,加一个判断即可:
//只检测是否有四个角度的改变
if( orientation > 350 || orientation< 10 ) { //0度
orientation = 0;
}
else if( orientation > 80 &&orientation < 100 ) { //90度
orientation= 90;
}
else if( orientation > 170 &&orientation < 190 ) { //180度
orientation= 180;
}
else if( orientation > 260 &&orientation < 280 ) { //270度
orientation= 270;
}
else {
return;
}
Log.i("MyOrientationDetector ","onOrientationChanged:"+orientation);
关于屏幕旋转的处理,就说到这儿了,本文只是提纲挈领地介绍了屏幕旋转处理的各个方面,具体的细节可以网上搜索相关的关键词或者查看API手册,这里就不再赘述了。
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
