分享

简单说说Android G-sensor 的优化

 wanwanstudy 2012-02-09
0

近期在2.2中解决某个G-sensor的Bug的时候,意外的发现2.3其实已经对这类问题进行了优化,借鉴于2.3的源码,给了我不少帮助。 2.3中主要是扩展了对旋屏180°的扩展,这个也许对手机来说没什么实际作用,但是对于平板电脑,却意味深长喽!!!

首先是 int getCurrentRotation() ,不仅仅只针对mRotation ,还增加了对lastRotation的考究,单单就是这点,就方便了我们做很多事情,可以很方便的增加很多判断和条件,来达到我们想要实现的目的。代码如下:

1int getCurrentRotation(int lastRotation) {
2            if (mTiltDistrust > 0) {
3                // we really don't know the current orientation, so trust what's currently displayed
4                mRotation = SURFACE_TO_INTERNAL_ROTATION[lastRotation];
5            }
6            return INTERNAL_TO_SURFACE_ROTATION[mRotation];
7        }

最值得一提的就是下面的代码了:

01// Mapping our internal aliases into actual Surface rotation values
02private static final int[] INTERNAL_TO_SURFACE_ROTATION = new int[] {
03    Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_270,
04    Surface.ROTATION_180};
05 
06// Mapping Surface rotation values to internal aliases.
07private static final int[] SURFACE_TO_INTERNAL_ROTATION = new int[] {
08    ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270};
09 
10// Threshold ranges of orientation angle to transition into other orientation states.
11// The first list is for transitions from ROTATION_0, ROTATION_90, ROTATION_270,
12// and then ROTATION_180.
13// ROTATE_TO defines the orientation each threshold range transitions to, and must be kept
14// in sync with this.
15// We generally transition about the halfway point between two states with a swing of 30
16// degrees for hysteresis.
17private static final int[][][] THRESHOLDS = new int[][][] {
18        {{60, 180}, {180, 300}},
19        {{0, 30}, {195, 315}, {315, 360}},
20        {{0, 45}, {45, 165}, {330, 360}},
21 
22        // Handle situation where we are currently doing 180 rotation
23        // but that is no longer allowed.
24        {{0, 45}, {45, 135}, {135, 225}, {225, 315}, {315, 360}},
25};
26// See THRESHOLDS
27private static final int[][] ROTATE_TO = new int[][] {
28        {ROTATION_90, ROTATION_270},
29        {ROTATION_0, ROTATION_270, ROTATION_0},
30        {ROTATION_0, ROTATION_90, ROTATION_0},
31        {ROTATION_0, ROTATION_90, ROTATION_0, ROTATION_270, ROTATION_0},
32};
33 
34// Thresholds that allow all 4 orientations.
35private static final int[][][] THRESHOLDS_WITH_180 = new int[][][] {
36    {{60, 165}, {165, 195}, {195, 300}},
37    {{0, 30}, {165, 195}, {195, 315}, {315, 360}},
38    {{0, 45}, {45, 165}, {165, 195}, {330, 360}},
39    {{0, 45}, {45, 135}, {225, 315}, {315, 360}},
40};
41// See THRESHOLDS_WITH_180
42private static final int[][] ROTATE_TO_WITH_180 = new int[][] {
43    {ROTATION_90, ROTATION_180, ROTATION_270},
44    {ROTATION_0, ROTATION_180, ROTATION_90, ROTATION_0},
45    {ROTATION_0, ROTATION_270, ROTATION_180, ROTATION_0},
46    {ROTATION_0, ROTATION_90, ROTATION_270, ROTATION_0},
47};

注释写的清晰明了,我把它加入到2.2中,稍作修实现了我所想实现的目标,让G-sensor在某种特殊情况x,y,z 重定义N次得情况下,稳定工作。这还是要归功于,GOOGLE在x,y,z三维检测对0,90,180,270,4方向的支持。       

另外,这也给大家提个醒,有什么没有解决的bug,不如看看高版本有没有进行优化和改变,也许会有意外收获~

文章出处:http://blog.csdn.net/yiyaaixuexi/article/details/6333803

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多