由于工作上的需要,特地写了这么一份关于调试g-sensor的内容.
1.首先确定你要调试的设备的屏幕的横竖屏如何设置。
在代码中我们主要的流程如下:
os 启动后 :
WindowManagerService.java中ENABLE_SCREEN
–>performEnableScreen()
–>mPolicy.enableScreenAfterBoot()/setRotation()
–>setRotationUnchecked()
–>PhoneWindowManager.java中的rotationForOrientationLw()
–>Surface.setOrientation()
基本上流程就是如上,只要稍微跟踪一下就可以了。
下面大概对上面主要code进行注释说明:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
下面在对rotationForOrientationLw()进行解析一下:
在setRotationUncheckedLocked()中的 mPolicy.rotationForOrientationLw()如是:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
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。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
这里对calculateNewRotation进行分析前必须先对SensorEventListenerImpl类中的一些变量先进行解释:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
对上面的变量稍微了解后对下面的分析就很简单了。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
onOrientationChanged()的实现在PhoneWindowManager.java 中,如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
基本上整个流程到此结束。
转载时请注明出处和作者
文章出处:http://www.code007.org/
作者:Code007