Android 2.1实现屏幕不同方向旋转

 

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.        }
哈哈,四个方向旋转都可以了!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值