http://www.linuxidc.com/Linux/2011-08/40119.htm
最近调g-sensor的过程中发现Android2.1在设置界面横竖屏幕旋转时只有两个方向,而且板子横着时显示竖屏,竖着时显示横屏(前一版硬件可没这个问题,看来是硬件工程师将g-sensor模块贴片方向改变)。
为了解决横竖颠倒的问题,干脆用最简单的方法:在g-sensor驱动中在input_report_abs()函数上报前将x、y轴交换,z轴不变。
1.short temp;
1.temp = x;
2.x = y;
3.y = temp;
4.input_report_abs(mma_abs_dev, ABS_X, x);
5.input_report_abs(mma_abs_dev, ABS_Y, y);
6.input_report_abs(mma_abs_dev, ABS_Z, z);
7.input_report_abs(mma_abs_dev, ABS_RX, tilt);
8.input_sync(mma_abs_dev);
横竖颠倒解决了,但只有两个方向旋转(0度、90度),我可是需要四个方向啊,研究了下framework代码,发现Android是支持四个方向的,分别是:ROTATION_0、ROTATION_90、ROTATION_180、ROTATION_270,在板子上试了下Android2.2发现有3个方向,而Android2.1只有2个,既然是版本的问题,那就直接修改框架层代码,找了半天终于发现只要修改文件frameworks/base/core/java/android/view/WindowOrientationListener.java
修改代码:
1.public void onSensorChanged(SensorEvent event) {
1. float[] values = event.values;
2. float X = values[_DATA_X];
3. float Y = values[_DATA_Y];
4. float Z = values[_DATA_Z];
5. float OneEightyOverPi = 57.29577957855f;
6. float gravity = (float) Math.sqrt(X*X+Y*Y+Z*Z);
7. float zyangle = (float)Math.asin(Z/gravity)*OneEightyOverPi;
8. int rotation = -1;
9. if ((zyangle <= PIVOT_UPPER) && (zyangle >= PIVOT_LOWER)) {
10. // Check orientation only if the phone is flat enough
11. // Don't trust the angle if the magnitude is small compared to the y value
1. float angle = (float)Math.atan2(Y, -X) * OneEightyOverPi;
13. int orientation = 90 - (int)Math.round(angle);
14. // normalize to 0 - 359 range
1. while (orientation >= 360) {
16. orientation -= 360;
17. }
18. while (orientation < 0) {
19. orientation += 360;
20. }
21. // Orientation values between LANDSCAPE_LOWER and PL_LOWER
22. // are considered landscape.
23. // Ignore orientation values between 0 and LANDSCAPE_LOWER
24. // For orientation values between LP_UPPER and PL_LOWER,
25. // the threshold gets set linearly around PIVOT.
26. /*
1. if ((orientation >= PL_LOWER) && (orientation <= LP_UPPER)) {
2. float threshold;
3. float delta = zyangle - PIVOT;
4. if (mSensorRotation == Surface.ROTATION_90) {
5. if (delta < 0) {
6. // Delta is negative
7. threshold = LP_LOWER - (LP_LF_LOWER * delta);
8. } else {
9. threshold = LP_LOWER + (LP_LF_UPPER * delta);
10. }
11. rotation = (orientation >= threshold) ? Surface.ROTATION_0 : Surface.ROTATION_90;
12. } else {
13. if (delta < 0) {
14. // Delta is negative
15. threshold = PL_UPPER+(PL_LF_LOWER * delta);
16. } else {
17. threshold = PL_UPPER-(PL_LF_UPPER * delta);
18. }
19. rotation = (orientation <= threshold) ? Surface.ROTATION_90: Surface.ROTATION_0;
20. }
21. } else if ((orientation >= LANDSCAPE_LOWER) && (orientation < LP_LOWER)) {
22. rotation = Surface.ROTATION_90;
23. } else if ((orientation >= PL_UPPER) || (orientation <= PORTRAIT_LOWER)) {
24. rotation = Surface.ROTATION_0;
25. }
26. */
27. if(orientation > 325 || orientation <= 45){
28. rotation = Surface.ROTATION_0;
29. }else if(orientation > 45 && orientation <= 135){
30. rotation = Surface.ROTATION_270;
31. }else if(orientation > 135 && orientation < 225){
32. rotation = Surface.ROTATION_180;
33. }else {
34. rotation = Surface.ROTATION_90;
35. }
36. if ((rotation != -1) && (rotation != mSensorRotation)) {
37. mSensorRotation = rotation;
38. onOrientationChanged(mSensorRotation);
39. }
40. }
41. }
哈哈,四个方向旋转都可以了!