您当前的位置: 首页 > 

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

实时获取当前屏幕方向

蓝不蓝编程 发布时间:2018-10-23 17:21:39 ,浏览量:0

背景

开发中,有时需要获取当前屏幕的方向,所以单独写了个demo,方便大家参考。

Demo源代码:

https://gitee.com/hspbc/screenOrientation.git

主要代码:
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 = findViewById(R.id.textView);
        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  315)) {
                    rotation = 0;
                } else if ((rotation > 45) && (rotation  135) && (rotation  225) && (rotation  315 || degrees  45 && degrees  135 && degrees  225 && degrees             
关注
打赏
1639405877
查看更多评论
0.0372s