zoukankan      html  css  js  c++  java
  • Android屏幕重力感应旋转

    public class MainActivity extends AppCompatActivity {
    
      private MyOrientoinListener myOrientoinListener;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            myOrientoinListener = new MyOrientoinListener(this);
            boolean autoRotateOn = (android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1);
            //检查系统是否开启自动旋转
            if (autoRotateOn) {
                myOrientoinListener.enable();
            }
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //销毁时取消监听
            myOrientoinListener.disable();
        }
    
        class MyOrientoinListener extends OrientationEventListener {
            public MyOrientoinListener(Context context) {
            super(context);
        }
    
        public MyOrientoinListener(Context context, int rate) {
            super(context, rate);
        }
    
        @Override
        public void onOrientationChanged(int orientation) {
            int screenOrientation = getResources().getConfiguration().orientation;
    
            if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) {    //设置竖屏
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
            } else if (orientation > 225 && orientation < 315) { //设置横屏
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            } else if (orientation > 45 && orientation < 135) {// 设置反向横屏
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
            } else if (orientation > 135 && orientation < 225) { //反向竖屏
                if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
            }
        }
    }
  • 相关阅读:
    Quartz.Net 学习随手记之04 构建Windows Service承载服务
    Quartz.Net 学习随手记之03 配置文件
    SQL Server问题之计算机名称更改后无法登陆本地数据库
    SQL Server问题之The remote procedure call failed. [0x800706be]
    跨框架菜单menuG5使用
    DLink 524M经常断线、掉线问题的解决
    MSChart使用导航之开发
    ReSharper制作Template帮助我们快速输入代码
    网站右下角弹出通知效果的制作
    Dell6400拆卸与维护
  • 原文地址:https://www.cnblogs.com/yxc6123/p/8385697.html
Copyright © 2011-2022 走看看