Brief Introduction to Android G-sensor Optimization

Source: Internet
Author: User

Recently, when we solved a g-sensor bug in 2.2, we accidentally found that 2.3 has actually optimized this type of problem. The source code of 2.3 has helped me a lot. 2.3 is mainly an extension of 180 ° for the rotating screen, which may have no practical effect on mobile phones, but it is meaningful for tablet computers !!!

The first is int getcurrentrotation (), not only for mrotation, but also for lastrotation. This alone makes it easier for us to do a lot of things, we can easily add a lot of judgments and conditions to achieve our goal. The Code is as follows:

 

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];
}

 

The following code is worth mentioning:

 

// 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 },
};

Note writing is clear and clear. I add it to 2.2, and a slight revision achieves what I want to achieve, so that G-sensor can, Z can work stably when it is redefined n times. This is due to Google's support for orientation 0, 90, 180,270, and 4 in X, Y, and Z 3D detection.

In addition, this also reminds you of any bugs that have not been solved. It is better to see whether the higher version has been optimized or changed, and some unexpected gains may occur ~

 

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.