由于工作上的需要,特地寫了這麼一份關于調試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