最近调g-sensor的过程中发现Android2.1在设置界面横竖屏幕旋转时只有两个方向,而且板子横着时显示竖屏,竖着时显示横屏(前一版硬件可没这个问题,看来是硬件工程师将g-sensor模块贴片方向改变)。
为了解决横竖颠倒的问题,干脆用最简单的方法:在g-sensor驱动中在input_report_abs()函数上报前将x、y轴交换,z轴不变。
- short temp;
- temp = x;
- x = y;
- y = temp;
- input_report_abs(mma_abs_dev, ABS_X, x);
- input_report_abs(mma_abs_dev, ABS_Y, y);
- input_report_abs(mma_abs_dev, ABS_Z, z);
- input_report_abs(mma_abs_dev, ABS_RX, tilt);
- 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
修改代码:
- public void onSensorChanged(SensorEvent event) {
- float[] values = event.values;
- float X = values[_DATA_X];
- float Y = values[_DATA_Y];
- float Z = values[_DATA_Z];
- float OneEightyOverPi = 57.29577957855f;
- float gravity = (float) Math.sqrt(X*X+Y*Y+Z*Z);
- float zyangle = (float)Math.asin(Z/gravity)*OneEightyOverPi;
- int rotation = -1;
- if ((zyangle <= PIVOT_UPPER) && (zyangle >= PIVOT_LOWER)) {
- // Check orientation only if the phone is flat enough
- // Don't trust the angle if the magnitude is small compared to the y value
- float angle = (float)Math.atan2(Y, -X) * OneEightyOverPi;
- int orientation = 90 - (int)Math.round(angle);
- // normalize to 0 - 359 range
- while (orientation >= 360) {
- orientation -= 360;
- }
- while (orientation < 0) {
- orientation += 360;
- }
- // Orientation values between LANDSCAPE_LOWER and PL_LOWER
- // are considered landscape.
- // Ignore orientation values between 0 and LANDSCAPE_LOWER
- // For orientation values between LP_UPPER and PL_LOWER,
- // the threshold gets set linearly around PIVOT.
- /*
- if ((orientation >= PL_LOWER) && (orientation <= LP_UPPER)) {
- float threshold;
- float delta = zyangle - PIVOT;
- if (mSensorRotation == Surface.ROTATION_90) {
- if (delta < 0) {
- // Delta is negative
- threshold = LP_LOWER - (LP_LF_LOWER * delta);
- } else {
- threshold = LP_LOWER + (LP_LF_UPPER * delta);
- }
- rotation = (orientation >= threshold) ? Surface.ROTATION_0 : Surface.ROTATION_90;
- } else {
- if (delta < 0) {
- // Delta is negative
- threshold = PL_UPPER+(PL_LF_LOWER * delta);
- } else {
- threshold = PL_UPPER-(PL_LF_UPPER * delta);
- }
- rotation = (orientation <= threshold) ? Surface.ROTATION_90: Surface.ROTATION_0;
- }
- } else if ((orientation >= LANDSCAPE_LOWER) && (orientation < LP_LOWER)) {
- rotation = Surface.ROTATION_90;
- } else if ((orientation >= PL_UPPER) || (orientation <= PORTRAIT_LOWER)) {
- rotation = Surface.ROTATION_0;
- }
- */
- if(orientation > 325 || orientation <= 45){
- rotation = Surface.ROTATION_0;
- }else if(orientation > 45 && orientation <= 135){
- rotation = Surface.ROTATION_270;
- }else if(orientation > 135 && orientation < 225){
- rotation = Surface.ROTATION_180;
- }else {
- rotation = Surface.ROTATION_90;
- }
- if ((rotation != -1) && (rotation != mSensorRotation)) {
- mSensorRotation = rotation;
- onOrientationChanged(mSensorRotation);
- }
- }
- }
哈哈,四个方向旋转都可以了!
阅读(3622) | 评论(0) | 转发(2) |