首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

利用GSensor让荧屏实现360度旋转

2012-08-26 
利用GSensor让屏幕实现360度旋转?public void onSensorChanged(SensorEvent event) {float[] values eve

利用GSensor让屏幕实现360度旋转

?

在Froyo中,对上述算法进行了修改,让其报出270度的旋转方向。

?

?

Android Sensor 屏幕360度旋转实现2010-09-17 10:51

修改下面函数 void android.view.WindowOrientationListener.SensorEventListenerImpl.onSensorChanged(android.hardware.SensorEvent event)


??????????? float[] values = event.values;
?????????????
??????????? float X = values[_DATA_X];
??????????? float Y = values[_DATA_Y];
??????????? float Z = values[_DATA_Z];
??????????? //For fixing the problem of Sensor change the window orientation error but the sensor game is no problem.
??????????? float OneEightyOverPi = 57.29577957855f;
??????????? float gravity = (float) Math.sqrt(X*X+Y*Y+Z*Z);
??????????? float zyangle = (float)Math.asin(Z/gravity)*OneEightyOverPi;
??????????? int rotation = -1;
??????????? if ((zyangle <= PIVOT_UPPER) && (zyangle >= PIVOT_LOWER)) {
??????????????? // Check orientation only if the phone is flat enough
??????????????? // Don't trust the angle if the magnitude is small compared to the y value
??????????????? float angle = (float)Math.atan2(Y, -X) * OneEightyOverPi;
??????????????? int orientation = 90 - (int)Math.round(angle);
??????????????? // normalize to 0 - 359 range
??????????????? while (orientation >= 360) {
??????????????????? orientation -= 360;
??????????????? }
??????????????? while (orientation < 0) {
??????????????????? orientation += 360;
??????????????? }
??????????????? Log.i("Tiger","orientation="+orientation);
???????????????? //确定由角度与屏幕方向的对应范围
??????????????? if(orientation > 325 || orientation <= 45){
??????????????? rotation = Surface.ROTATION_0;
??????????????? }else if(orientation > 45 && orientation <= 135){
??????????????? rotation = Surface.ROTATION_270;
??????????????? }else if(orientation > 135 && orientation < 225){
??????????????? rotation = Surface.ROTATION_180;
??????????????? }else {
??????????????? rotation = Surface.ROTATION_90;
??????????????? }
???????????????
??????????????? Log.i("Tiger","mSensorRotation="+mSensorRotation+"??? , rotation="+rotation);
??????????????? if ((rotation != -1) && (rotation != mSensorRotation)) {
??????????????????? mSensorRotation = rotation;
??????????????????? onOrientationChanged(mSensorRotation);
??????????????? }
??????????? }

热点排行