Chinaunix首页 | 论坛 | 博客
  • 博客访问: 430399
  • 博文数量: 125
  • 博客积分: 2066
  • 博客等级: 大尉
  • 技术积分: 1032
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-16 14:06
文章分类

全部博文(125)

文章存档

2011年(125)

分类:

2011-06-02 13:18:00

最近调g-sensor的过程中发现Android2.1在设置界面横竖屏幕旋转时只有两个方向,而且板子横着时显示竖屏,竖着时显示横屏(前一版硬件可没这个问题,看来是硬件工程师将g-sensor模块贴片方向改变)。
 
为了解决横竖颠倒的问题,干脆用最简单的方法:在g-sensor驱动中在input_report_abs()函数上报前将x、y轴交换,z轴不变。
  1. short temp;
  2. temp = x;
  3. x = y;
  4. y = temp;
  5. input_report_abs(mma_abs_dev, ABS_X, x);    
  6. input_report_abs(mma_abs_dev, ABS_Y, y);    
  7. input_report_abs(mma_abs_dev, ABS_Z, z);
  8. input_report_abs(mma_abs_dev, ABS_RX, tilt);
  9. 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) {
  2.             float[] values = event.values;
  3.             float X = values[_DATA_X];
  4.             float Y = values[_DATA_Y];
  5.             float Z = values[_DATA_Z];
  6.             float OneEightyOverPi = 57.29577957855f;
  7.             float gravity = (float) Math.sqrt(X*X+Y*Y+Z*Z);
  8.             float zyangle = (float)Math.asin(Z/gravity)*OneEightyOverPi;
  9.             int rotation = -1;
  10.             if ((zyangle <= PIVOT_UPPER) && (zyangle >= PIVOT_LOWER)) {
  11.                 // Check orientation only if the phone is flat enough
  12.                 // Don't trust the angle if the magnitude is small compared to the y value
  13.                 float angle = (float)Math.atan2(Y, -X) * OneEightyOverPi;
  14.                 int orientation = 90 - (int)Math.round(angle);
  15.                 // normalize to 0 - 359 range
  16.                 while (orientation >= 360) {
  17.                     orientation -= 360;
  18.                 }
  19.                 while (orientation < 0) {
  20.                     orientation += 360;
  21.                 }
  22.                 // Orientation values between LANDSCAPE_LOWER and PL_LOWER
  23.                 // are considered landscape.
  24.                 // Ignore orientation values between 0 and LANDSCAPE_LOWER
  25.                 // For orientation values between LP_UPPER and PL_LOWER,
  26.                 // the threshold gets set linearly around PIVOT.
  27.         /*        
  28.                 if ((orientation >= PL_LOWER) && (orientation <= LP_UPPER)) {
  29.                     float threshold;
  30.                     float delta = zyangle - PIVOT;
  31.                     if (mSensorRotation == Surface.ROTATION_90) {
  32.                         if (delta < 0) {
  33.                             // Delta is negative
  34.                             threshold = LP_LOWER - (LP_LF_LOWER * delta);
  35.                         } else {
  36.                             threshold = LP_LOWER + (LP_LF_UPPER * delta);
  37.                         }
  38.                         rotation = (orientation >= threshold) ? Surface.ROTATION_0 : Surface.ROTATION_90;
  39.                     } else {
  40.                         if (delta < 0) {
  41.                             // Delta is negative
  42.                             threshold = PL_UPPER+(PL_LF_LOWER * delta);
  43.                         } else {
  44.                             threshold = PL_UPPER-(PL_LF_UPPER * delta);
  45.                         }
  46.                         rotation = (orientation <= threshold) ? Surface.ROTATION_90: Surface.ROTATION_0;
  47.                     }
  48.                 } else if ((orientation >= LANDSCAPE_LOWER) && (orientation < LP_LOWER)) {
  49.                     rotation = Surface.ROTATION_90;
  50.                 } else if ((orientation >= PL_UPPER) || (orientation <= PORTRAIT_LOWER)) {
  51.                     rotation = Surface.ROTATION_0;
  52.                 }
  53.         */
  54.                 if(orientation > 325 || orientation <= 45){
  55.                     rotation = Surface.ROTATION_0;
  56.                 }else if(orientation > 45 && orientation <= 135){
  57.                     rotation = Surface.ROTATION_270;
  58.                 }else if(orientation > 135 && orientation < 225){
  59.                     rotation = Surface.ROTATION_180;
  60.                 }else {
  61.                     rotation = Surface.ROTATION_90;
  62.                 }        
  63.                 if ((rotation != -1) && (rotation != mSensorRotation)) {
  64.                     mSensorRotation = rotation;
  65.                     onOrientationChanged(mSensorRotation);
  66.                 }
  67.             }
  68.         }

哈哈,四个方向旋转都可以了!

阅读(1165) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~