Discussion on the optimization of Android G-sensor

Source: Internet
Author: User
Tags aliases final

Recently in 2.2 to solve a g-sensor bug, the unexpected discovery of 2.3 in fact, this kind of problem has been optimized for reference to 2.3 of the source code, gave me a lot of help. 2.3 is mainly extended to the expansion of the rotary screen 180°, this may not be practical for the mobile phone, but for the tablet, but it's meaningful!!!

The first is int getcurrentrotation (), not only for mrotation, but also added to the lastrotation of the elegant, just this, it is convenient for us to do a lot of things, can be very convenient to add a lot of judgments and conditions, To achieve what we want to achieve. The code is as follows:

int getcurrentrotation (int lastrotation) {  
      if (Mtiltdistrust > 0) {  
          //We really don ' t know the current Orienta tion, so trust what ' s currently displayed  
          mrotation = Surface_to_internal_rotation[lastrotation];  
      }  
      return internal_to_surface_rotation[mrotation];  
  }

The most important thing to mention is the following code:

Mapping our internal aliases into actual Surface rotation values private static final int[] Internal_to_surface_rota  
     
tion = 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, Rotatio  
     
N_270};  
Threshold ranges of orientation angle to transition into the other orientation.  
The the 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 is kept//in sync with this.  
We generally transition about the halfway point between two states with a swing of all//degrees for hysteresis. private static final int[][][] thresholds = new int[][][] {{60, 180}, {180, 300}}, {{0, 30}, {195,315}, {315, 360}}, {{0, {}, {, 165}, {%, 360}},//Handle situation where we are currently  
        Doing 180 rotation//But is no longer allowed.  
{{0, 45}, {45, 135}, {135, 225}, {225, 315}, {315, 360}},};  
        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}},}; 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, ROTA tion_180, rotation_0}, {rotation_0, rotation_90, rotation_270, rotation_0},};

Note written clearly, I added it to 2.2, slightly to achieve the goal I want to achieve, let G-sensor in a special situation x,y,z redefine n times, stable work. This is thanks to Google's support for the 0,90,180,270,4 direction in x,y,z three-dimensional testing.

In addition, this also gives us a wake-up call, what has not resolved the bug, better to see if the high version has been optimized and changed, there may be unexpected harvest

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.