你好,游客 登录 注册 搜索
背景:
阅读新闻

Android开发实践:屏幕旋转的处理

[日期:2013-09-25] 来源:Linux社区  作者:ticktick [字体:大 中 小]

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

linux
相关资讯       Android屏幕旋转 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款