android 传感器获取方向总结
来源:互联网 发布:手机编辑pdf 软件 编辑:程序博客网 时间:2024/06/04 00:48
在2.2中可以设置屏幕的方向为反转横屏:setRequestedOrientation(8);,因为系统没有公开出这个参数的设置,不过在源码里面已经定义了SCREEN_ORIENTATION_REVERSE_LANDSCAPE这个参数,但是无法固定屏幕的方向为反转横屏即右横屏。在设置屏幕方向为右横屏的时候 还得注意一点,需要将activity设置为android:theme="@android:style/Theme.Translucent",否则即使设置了setRequestedOrientation(8);也不会显示为右横屏。
虽然能根据旋转角度获取到屏幕的方向,但是在2.1的时候根本无法设置为右横屏,下面记录一点从源码抠出来的根据传感器根据角度获取屏幕方向。
传感器监听:
class SubSensorListener implements SensorEventListener {private static final int _DATA_X = 0;private static final int _DATA_Y = 1;private static final int _DATA_Z = 2;public static final int ORIENTATION_UNKNOWN = -1;private Handler handler;public SubSensorListener (Handler handler) {this.handler = handler;}public void onAccuracyChanged(Sensor arg0, int arg1) {}public void onSensorChanged(SensorEvent event) {float[] values = event.values;int orientation = ORIENTATION_UNKNOWN;float X = -values[_DATA_X];float Y = -values[_DATA_Y];float Z = -values[_DATA_Z];float magnitude = X * X + Y * Y;// Don't trust the angle if the magnitude is small compared to the y valueif (magnitude * 4 >= Z * Z) {float OneEightyOverPi = 57.29577957855f;float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;orientation = 90 - (int) Math.round(angle);// normalize to 0 - 359 rangewhile (orientation >= 360) {orientation -= 360;}while (orientation < 0) {orientation += 360;}}if (handler != null) {handler.obtainMessage(123, orientation, 0).sendToTarget();}}
由于方向的改变会不断发生变化,所以我们需要一个handler来处理,下面是handler通过对旋转角度的处理获得方向:
@Overridepublic void handleMessage(Message msg) {if (msg.what == 123) {int orientation = msg.arg1;if (orientation > 45 && orientation < 135) {// SCREEN_ORIENTATION_REVERSE_LANDSCAPEactivity.setRequestedOrientation(8);} else if (orientation > 135 && orientation < 225) {// SCREEN_ORIENTATION_REVERSE_PORTRAITactivity.setRequestedOrientation(9);} else if (orientation > 225 && orientation < 315) {// SCREEN_ORIENTATION_LANDSCAPEactivity.setRequestedOrientation(0);} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {// SCREEN_ORIENTATION_PORTRAITactivity.setRequestedOrientation(1);}}}
- android 传感器获取方向总结
- Android开发传感器应用---获取方向传感器
- Android方向传感器获取正确方向
- Android 传感器之方向传感器
- Android 方向传感器
- android方向传感器
- Android方向传感器
- android 方向传感器
- Android笔记 方向传感器
- Android传感器坐标轴方向
- android 方向传感器应用
- Android:方向传感器
- Android方向传感器
- Android:方向传感器
- Android-传感器-方向
- Android方向传感器
- 获取Android设备的方向 ,使用加速度重力传感器
- Android传感器——方向传感器TYPE_ORIENTATION
- 关于在clob上的长字符使用replace替换某些词会出现几行乱码?
- powerdesign,如何解决PDM生成sql脚本的过程中,无法生成外键约束
- Hadoop生态图谱
- 使用XCode调试
- android多语言与国际化
- android 传感器获取方向总结
- 构建ASP.NET网站十大必备工具
- Android中Handler的使用方法——在子线程中更新界面
- 技术文档下载网站大全
- Learning Perl
- 使用maven部署项目到tomcat 7
- 【Android】向sdcard中写入文件
- Android笔记--handler机制
- jackson:SerializationConfig.Feature 的枚举常量 WRITE_NULL_MAP_VALUES