近期在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}, };