您的位置:首页 > 移动开发 > Android开发

关于android2.3调试g-sensor

2012-02-16 15:59 495 查看

关于android2.3调试g-sensor

由于工作上的需要,特地写了这么一份关于调试g-sensor的内容.

1.首先确定你要调试的设备的屏幕的横竖屏如何设置。

在代码中我们主要的流程如下:

os 启动后 :

WindowManagerService.java中ENABLE_SCREEN

–>performEnableScreen()

–>mPolicy.enableScreenAfterBoot()/setRotation()

–>setRotationUnchecked()

–>PhoneWindowManager.java中的rotationForOrientationLw()

–>Surface.setOrientation()

基本上流程就是如上,只要稍微跟踪一下就可以了。

下面大概对上面主要code进行注释说明:

?
下面在对rotationForOrientationLw()进行解析一下:

在setRotationUncheckedLocked()中的 mPolicy.rotationForOrientationLw()如是:

?
2.如果g-sensor在旋转上有不旋转的方向或者方向不是很灵敏,则我们从下面进行分析:

主要流程如下:

–>WindowOrientationListener.java中的onSensorChanged()

–>computeNewOrientation()

–>filterOrientation()

–>calculateNewRotation()

calculateNewRotation()

–>mOrientationListener.onOrientationChanged()

PhoneWindowManager.java 中的onOrientationChanged()

–>mWindowManager.setRotation()

首先在android中的x,y,z定义如下摘自http://developer.android.com/reference/android/hardware/SensorEvent.html :

Class Overview

This class represents a Sensor event and holds informations such as the sensor’s type, the time-stamp, accuracy and of course the sensor’s data.

Definition of the coordinate system used by the SensorEvent API.

The coordinate-system is defined relative to the screen of the phone in its default orientation. The axes are not swapped when the device’s screen orientation changes.

The X axis is horizontal and points to the right, the Y axis is vertical and points up and the Z axis points towards the outside of the front face of the screen. In this system, coordinates behind the screen have negative Z values.

Sensors coordinate-system diagram.





Note: This coordinate system is different from the one used in the Android 2D APIs where the origin is in the top-left corner.

frameworks/base/core/java/android/view/WindowOrientationListener.java

WindowOrientationListener.java 是一个abstract class,它主要是把从gsensor获取到的数据转化为orientation.

每次sensor有进行改变时都会调用到以下函数进行计算Orientation。

?
这里对calculateNewRotation进行分析前必须先对SensorEventListenerImpl类中的一些变量先进行解释:

?
对上面的变量稍微了解后对下面的分析就很简单了。

?
onOrientationChanged()的实现在PhoneWindowManager.java 中,如下:

?
基本上整个流程到此结束。

转载时请注明出处和作者

文章出处:http://www.code007.org/

作者:Code007

---------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: