android 获取当前屏幕方向

直接上源码:

package com.z.currorientation;

import android.content.Context;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private final String TAG = MainActivity.class.getSimpleName();
    private CameraOrientationListener orientationListener;
    private TextView txt;
    /**
     * 当前屏幕旋转角度
     */
    private int mOrientation = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txt = (TextView) findViewById(R.id.txt);
        orientationListener = new CameraOrientationListener(this);
        orientationListener.enable();

        startOrientationChangeListener();
    }

    /**
     * 启动屏幕朝向改变监听函数 用于在屏幕横竖屏切换时改变保存的图片的方向
     */
    private void startOrientationChangeListener() {
        OrientationEventListener mOrEventListener = new OrientationEventListener(this) {
            @Override
            public void onOrientationChanged(int rotation) {
                Log.i(TAG, "当前屏幕手持角度方法:" + rotation + "°");
                if (((rotation >= 0) && (rotation <= 45)) || (rotation > 315)) {
                    rotation = 0;
                } else if ((rotation > 45) && (rotation <= 135)) {
                    rotation = 90;
                } else if ((rotation > 135) && (rotation <= 225)) {
                    rotation = 180;
                } else if ((rotation > 225) && (rotation <= 315)) {
                    rotation = 270;
                } else {
                    rotation = 0;
                }
                if (rotation == mOrientation)
                    return;
                mOrientation = rotation;

            }
        };
        mOrEventListener.enable();
    }

    /**
     * 当方向改变时,将调用侦听器onOrientationChanged(int)
     */
    private class CameraOrientationListener extends OrientationEventListener {

        private int mCurrentNormalizedOrientation;
        private int mRememberedNormalOrientation;

        public CameraOrientationListener(Context context) {
            super(context, SensorManager.SENSOR_DELAY_NORMAL);
        }

        @Override
        public void onOrientationChanged(final int orientation) {
            Log.i(TAG, "当前屏幕手持角度:" + orientation + "°");
            if (orientation != ORIENTATION_UNKNOWN) {
                mCurrentNormalizedOrientation = normalize(orientation);
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String str = "当前屏幕手持角度:" + orientation + "°\n当前屏幕手持方向:" + mCurrentNormalizedOrientation;
                    txt.setText(str);
                }
            });
        }

        private int normalize(int degrees) {
            if (degrees > 315 || degrees <= 45) {
                return 0;
            }

            if (degrees > 45 && degrees <= 135) {
                return 90;
            }

            if (degrees > 135 && degrees <= 225) {
                return 180;
            }

            if (degrees > 225 && degrees <= 315) {
                return 270;
            }

            throw new RuntimeException("The physics as we know them are no more. Watch out for anomalies.");
        }

        /**
         * 记录方向
         */
        public void rememberOrientation() {
            mRememberedNormalOrientation = mCurrentNormalizedOrientation;
        }

        /**
         * 获取当前方向
         *
         * @return
         */
        public int getRememberedNormalOrientation() {
            return mRememberedNormalOrientation;
        }
    }
}

10-16 16:20:11.256 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:355°
10-16 16:20:11.257 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:355°
10-16 16:20:12.318 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:356°
10-16 16:20:12.319 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:356°
10-16 16:20:12.506 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:355°
10-16 16:20:12.506 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:355°
10-16 16:20:12.881 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:356°
10-16 16:20:12.881 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:356°
10-16 16:20:14.131 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:355°
10-16 16:20:14.131 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:355°
10-16 16:20:14.319 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:356°
10-16 16:20:14.319 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:356°
10-16 16:20:29.194 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:359°
10-16 16:20:29.194 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:359°
10-16 16:20:29.381 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:355°
10-16 16:20:29.381 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:355°
10-16 16:20:29.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:359°
10-16 16:20:29.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:359°
10-16 16:20:29.818 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:357°
10-16 16:20:29.819 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:357°
10-16 16:20:31.006 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:356°
10-16 16:20:31.006 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:356°
10-16 16:20:31.193 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:357°
10-16 16:20:31.193 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:357°
10-16 16:20:31.444 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:359°
10-16 16:20:31.444 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:359°
10-16 16:20:31.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:339°
10-16 16:20:31.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:339°
10-16 16:20:31.818 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:312°
10-16 16:20:31.818 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:312°
10-16 16:20:32.006 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:311°
10-16 16:20:32.006 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:311°
10-16 16:20:32.256 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:313°
10-16 16:20:32.256 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:313°
10-16 16:20:32.444 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:314°
10-16 16:20:32.444 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:314°
10-16 16:20:32.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:327°
10-16 16:20:32.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:327°
10-16 16:20:32.818 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:6°
10-16 16:20:32.819 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:6°
10-16 16:20:33.069 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:1°
10-16 16:20:33.069 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:1°
10-16 16:20:33.256 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:352°
10-16 16:20:33.256 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:352°
10-16 16:20:33.444 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:358°
10-16 16:20:33.444 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:358°
10-16 16:20:33.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:15°
10-16 16:20:33.631 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:15°
10-16 16:20:33.819 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度方法:-1°
10-16 16:20:33.819 14881-14881/com.z.currorientation I/MainActivity: 当前屏幕手持角度:-1°

一般用于自定义相机的时候...

你可能感兴趣的:(android,基础)