重力传感器实现横竖屏切换

简介: 重力传感器实现横竖屏切换

1.获取传感器实例

SensorManager mSensorManager;
Sensor sensor;
ChangeOrientationHandler mHandler;
OrientationSensorListener listener;
 
mHandler = new ChangeOrientationHandler(this);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener = new OrientationSensorListener(mHandler);
mSensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
 
 
 
@Override
    protected void onPause() {
        mSensorManager.unregisterListener(listener);
        super.onPause();
    }

2.OrientationSensorListener监听 传感器数据的变化

按顺时针方向旋转手机,orientation增加过程为 0-90-180-270-360(0)

public class OrientationSensorListener implements SensorEventListener {
    private static final int _DATA_X = 0;
    private static final int _DATA_Y = 1;
    private static final int _DATA_Z = 2;
 
    public static final int ORIENTATION_UNKNOWN = -1;
 
    private Handler rotateHandler;
 
 
    public OrientationSensorListener(Handler handler) {
        rotateHandler = handler;
    }
 
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub
 
    }
 
    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        int orientation = ORIENTATION_UNKNOWN;
        float X = -values[_DATA_X];
        float Y = -values[_DATA_Y];
        float Z = -values[_DATA_Z];
        float magnitude = X * X + Y * Y;
        // Don't trust the angle if the magnitude is small compared to the y value
        if (magnitude * 4 >= Z * Z) {
            float OneEightyOverPi = 57.29577957855f;
            float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;
            orientation = 90 - (int) Math.round(angle);
            // normalize to 0 - 359 range
            while (orientation >= 360) {
                orientation -= 360;
            }
            while (orientation < 0) {
                orientation += 360;
            }
        }
 
        if (rotateHandler != null) {
            rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();
        }
 
    }
}

3.ChangeOrientationHandler 实现横竖屏的切换

public class ChangeOrientationHandler extends Handler {
    private Activity activity;
 
    public ChangeOrientationHandler(Activity ac) {
        super();
        activity = ac;
    }
 
    @Override
    public void handleMessage(Message msg) {
        if (msg.what == 888) {
            int orientation = msg.arg1;
            Log.e("orientation",orientation+"");
            if (orientation > 45 && orientation < 135) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
//                Log.d(MainActivity.TAG, "横屏翻转: ");
            } else if (orientation > 135 && orientation < 225) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
//                Log.d(MainActivity.TAG, "竖屏翻转: ");
            } else if (orientation > 225 && orientation < 315) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//                Log.d(MainActivity.TAG, "横屏: ");
            } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//                Log.d(MainActivity.TAG, "竖屏: ");
            }
        }
        super.handleMessage(msg);
    }
}


目录
相关文章
|
4天前
[光源频闪] Basler相机光源频闪设置操作说明
[光源频闪] Basler相机光源频闪设置操作说明
90 0
HMI-43-【节能模式】顶部标题栏和底部信息栏及灯光及启动动画
今天来实现以下节能模式的Title底部信息栏,灯光系统,以及启动动画。
HMI-43-【节能模式】顶部标题栏和底部信息栏及灯光及启动动画
|
9月前
|
C++ 计算机视觉 Python
Qt+C++跑马灯-指示灯-风扇-虚线灯带-动画仿真
这篇博客针对<<Qt+C++跑马灯-指示灯-风扇-虚线灯带-动画仿真>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
94 0
|
Android开发
4.2 锁屏小部件亮屏时不显示边框
4.2 锁屏小部件亮屏时不显示边框
84 0
4.2 锁屏小部件亮屏时不显示边框
距离感应器黑屏,如何一直亮屏
距离感应器黑屏,如何一直亮屏
51 0
iOS-横竖屏管理,支持所有方向旋转时用代码控制允许旋转&不允许旋转
iOS-横竖屏管理,支持所有方向旋转时用代码控制允许旋转&不允许旋转
194 0
Threejs实现卫星太阳板折叠动画,信号发射,姿态调整
Threejs实现卫星太阳板折叠动画,信号发射,姿态调整
255 0
Threejs实现卫星太阳板折叠动画,信号发射,姿态调整
鼠标控制物体旋转、移动、缩放(Unity3D)
Unity3D对于鼠标操作物体的旋转、移动、缩放的功能点使用的比较多。 今天就分享如何使用Unity实现鼠标对于物体的旋转、移动、缩放。