zoukankan      html  css  js  c++  java
  • Android Lock Screen Orientation

    一些与屏幕有关的基础知识:
    
    //这个是手机屏幕的旋转角度 
    final int rotation = this.getWindowManager().getDefaultDisplay().getOrientation();
    
    rotation值有:
    Surface.ROTATION_0 
    Surface.ROTATION_90
    Surface.ROTATION_180
    Surface.ROTATION_270
     
    
    //这个是读取当前Activity 的屏幕方向
    final int orientation = this.getResources().getConfiguration().orientation;
    
    orientation值有:
    Configuration.ORIENTATION_PORTRAIT
    Configuration.ORIENTATION_LANDSCAPE
    
    //这个是设置Activity 的屏幕方向,与AndroidManifest.xml中的android:screenOrientation="landscape"配置等同
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
    

      

    Build.VERSION:Various version strings.

    android.os.Build.VERSION.SDK_INT:The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.

    Build.VERSION_CODES:Enumeration of the currently known SDK version codes. These are the values that can be found in SDK. Version numbers increment monotonically with each official platform release.

    android.os.Build.VERSION_CODES.FROYO:June 2010: Android 2.2

    The REVERSE_LANDSCAPE and REVERSE_PORTRAIT appear after android 2.3,so we can not use them on android 2.2 and former devices.

    复制代码
    private void disableRotation()
        {       
            final int orientation = getResources().getConfiguration().orientation;
            final int rotation = getWindowManager().getDefaultDisplay().getOrientation();
    
            // Copied from Android docs, since we don't have these values in Froyo 2.2
            int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
            int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
    
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
            {
                SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            }
    
            if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
            {
                if (orientation == Configuration.ORIENTATION_PORTRAIT){
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                }
                else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
            else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) 
            {
                if (orientation == Configuration.ORIENTATION_PORTRAIT){
                    setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
                else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
                    setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
            }
        }
    复制代码

    enable the rotation:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

    About the rotation and orientation,on some devices,the Surface.ROTATION_0 corresponds to ORIENTATION_LANDSCAPE,but others,may ORIENTATION_PORTRAIT.So we have to so some work to judge it.

    However,for some reason ROTATION_90 corresponds to SCREEN_ORIENTATION_REVERSE_PORTRAIT on the Xoom if you use the above method.So another method appears:

    复制代码
     private void disableRotation()
        {       
            final int orientation = getResources().getConfiguration().orientation;
            final int rotation = getWindowManager().getDefaultDisplay().getOrientation();
    
            // Copied from Android docs, since we don't have these values in Froyo 2.2
            int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
            int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;
    
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
            {
                SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
                SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            }
    
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                if (rotation == Surface.ROTATION_0|| rotation == Surface.ROTATION_270) 
                    // 0 for phone, 270 for tablet
                {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                } else {
                    setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                }
            } else {
                if (rotation == Surface.ROTATION_90|| rotation == Surface.ROTATION_0) 
                    // 90 for phone, 0 for tablet
                {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                } else {
                    setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                }
            }
            
        }
    复制代码

    i'm not sure the note "//0 for phone, 270 for tablet" and "// 90 for phone, 0 for tablet" are right on any device,but at least it works well in my device.so I use the latter one.

    When you want to disable/enable the autorotation of the device ,you have to write the settings.

  • 相关阅读:
    【docker】关于docker中挂载的解释
    【linux】centos7安装使用rz/sz命令
    【docker】 docker容器内部安装vi命令
    【docker】centOS7上部署的mysql和spring boot服务,要求,mysql的时间、java程序服务的时间和宿主机的时间完全保持一致【修改mysql时区,临时和永久】【修改spring boot配置文件时区】【修改docker启动spring boot实例程序时区】
    【docker】docker部署spring boot项目在服务器上
    【spring boot】spring boot后台时间正确,返回给前台的时间不正确,和后台差8个小时
    html页面跳转传递参数
    SpringCloud服务如何在Eureka安全优雅的下线
    使用SpringBoot Admin监控SpringCloud微服务
    对actuator的管理端点进行ip白名单限制(springBoot添加filter)
  • 原文地址:https://www.cnblogs.com/janehlp/p/5249158.html
Copyright © 2011-2022 走看看