Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> 淺談Android G-sensor 的優化

淺談Android G-sensor 的優化

編輯:Android開發教程

近期在2.2中解決某個G-sensor的Bug的時候,意外的發現2.3其實已經對這類問題進行了優化,借鑒於2.3的源碼,給了我不少幫助。 2.3中主要是擴展了對旋屏180°的擴展,這個也許對手機來說沒什麼實際作用,但是對於平板電腦,卻意味深長喽!!!

首先是 int getCurrentRotation() ,不僅僅只針對mRotation ,還增加了對lastRotation的考究,單單就是這點,就方便了我們做很多事情,可以很方便的增加很多判斷和條件,來達到我們想要實現的目的。代碼如下:

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

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

// Mapping our internal aliases into actual Surface rotation values  
private static final int[] INTERNAL_TO_SURFACE_ROTATION = new int[] {  
    Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_270,  
    Surface.ROTATION_180};  
     
// Mapping Surface rotation values to internal aliases.  
private static final int[] SURFACE_TO_INTERNAL_ROTATION = new int[] {  
    ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270};  
     
// Threshold ranges of orientation angle to transition into other orientation states.  
// The first list is for transitions from ROTATION_0, ROTATION_90, ROTATION_270,  
// and then ROTATION_180.  
// ROTATE_TO defines the orientation each threshold range transitions to, and must be kept  
// in sync with this.  
// We generally transition about the halfway point between two states with a swing of 30  
// degrees for hysteresis.  
private static final int[][][] THRESHOLDS = new int[][][] {  
        {{60, 180}, {180, 300}},  
        {{0, 30}, {195, 315}, {315, 360}},  
        {{0, 45}, {45, 165}, {330, 360}},  
     
        // Handle situation where we are currently doing 180 rotation  
        // but that is no longer allowed.  
        {{0, 45}, {45, 135}, {135, 225}, {225, 315}, {315, 360}},  
};  
// See THRESHOLDS  
private static final int[][] ROTATE_TO = new int[][] {  
        {ROTATION_90, ROTATION_270},  
        {ROTATION_0, ROTATION_270, ROTATION_0},  
        {ROTATION_0, ROTATION_90, ROTATION_0},  
        {ROTATION_0, ROTATION_90, ROTATION_0, ROTATION_270, ROTATION_0},  
};  
     
// Thresholds that allow all 4 orientations.  
private static final int[][][] THRESHOLDS_WITH_180 = new int[][][] {  
    {{60, 165}, {165, 195}, {195, 300}},  
    {{0, 30}, {165, 195}, {195, 315}, {315, 360}},  
    {{0, 45}, {45, 165}, {165, 195}, {330, 360}},  
    {{0, 45}, {45, 135}, {225, 315}, {315, 360}},  
};  
// See THRESHOLDS_WITH_180  
private static final int[][] ROTATE_TO_WITH_180 = new int[][] {  
    {ROTATION_90, ROTATION_180, ROTATION_270},  
    {ROTATION_0, ROTATION_180, ROTATION_90, ROTATION_0},  
    {ROTATION_0, ROTATION_270, ROTATION_180, ROTATION_0},  
    {ROTATION_0, ROTATION_90, ROTATION_270, ROTATION_0},  
};

注釋寫的清晰明了,我把它加入到2.2中,稍作修實現了我所想實現的目標,讓G-sensor在某種特殊情況x,y,z 重定義N次得情況下,穩定工作。這還是要歸功於,GOOGLE在x,y,z三維檢測對0,90,180,270,4方向的支持。        

另外,這也給大家提個醒,有什麼沒有解決的bug,不如看看高版本有沒有進行優化和改變,也許會有意外收獲

查看本欄目更多精彩內容:http://www.bianceng.cn/OS/extra/

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved