【问题标题】:Android get Screen Size deprecated?Android 已弃用屏幕尺寸?
【发布时间】:2012-05-26 11:02:53
【问题描述】:

嘿,我需要在我的应用程序中获取屏幕的宽度。该应用程序将在 2.1 及更高版本上运行。我已经将它设置为如下所示。该方法已弃用,我可能应该使用 getSize 或其他方式。但问题是:这是否适用于 3.0+ 和 4.0+ 等 android 版本,还是会使应用程序崩溃。我之前在一个线程中使用过一个不推荐使用的方法,它使应用程序在冰淇淋设备上崩溃。下面的方法行吗?

 Display display = getWindowManager().getDefaultDisplay(); 
     int width = display.getWidth();
     int height = display.getHeight();

编辑:

我已经尝试了 getSize,但我没有让它工作:

  Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;

【问题讨论】:

    标签: android size deprecated


    【解决方案1】:

    我不确定,但这可能有效:

    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();
        int height = display.getHeight();
    } else {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
    }
    

    【讨论】:

      【解决方案2】:

      我不知道这些已弃用的方法是否适用于 Android 3 和 4。最好的判断方法是在模拟器上进行测试。

      但是,为了获得最大兼容性,这里最安全的方法是尝试使用反射的一种方法,然后回退到另一种。本质上,您可以制作自己的getSize() 版本,它不会失败。我无法测试这个 atm,但它可能看起来像这样:

      void overrideGetSize(Display display, Point outSize) {
          try {
            // test for new method to trigger exception
            Class pointClass = Class.forName("android.graphics.Point");
            Method newGetSize = Display.class.getMethod("getSize", new Class[]{ pointClass });
      
            // no exception, so new method is available, just use it
            newGetSize.invoke(display, outSize);
          } catch(NoSuchMethodException ex) {
            // new method is not available, use the old ones
            outSize.x = display.getWidth();
            outSize.y = display.getHeight();
          }
      }
      

      然后当然只是用类似的东西来调用它

      Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      overrideGetSize(display, size);
      

      【讨论】:

      • 为了通过反射得到一个方法,你必须传入一个包含其参数的类的数组。您必须指定参数以防方法重载。请参阅getMethod() 的文档。所以我们为Point 类创建了一个只有一个元素的数组,因为getSize() 只接受那个参数。这有帮助吗?
      • 或者你可以使用不折旧的Display getMetrics()
      • @JohanvdH - 你是对的。 getMetrics() 自 API 1 以来一直存在,看起来它返回相同的值。这可能是最简单的方法。
      【解决方案3】:

      我已经扩展了 Steve 的有用代码,这样 Eclipse 就不会给出任何警告或错误,并且我还对其进行了轻微的重组。由于 Point 类从 API 级别 1 开始就已经存在,我并没有看到通过反射创建它有什么好处。

        final static String mTAG = "MYTAG";
      
        // Cope with deprecated getWidth() and getHeight() methods
        Point getSize(Display xiDisplay) 
        {
          Point outSize = new Point();
          boolean sizeFound = false;
      
          try 
          {
            // Test if the new getSize() method is available
            Method newGetSize = 
              Display.class.getMethod("getSize", new Class[] { Point.class });
      
            // No exception, so the new method is available
            Log.d(mTAG, "Use getSize to find screen size");
            newGetSize.invoke(xiDisplay, outSize);
            sizeFound = true;
            Log.d(mTAG, "Screen size is " + outSize.x + " x " + outSize.y);
          } 
          catch (NoSuchMethodException ex) 
          {
            // This is the failure I expect when the deprecated APIs are not available
            Log.d(mTAG, "getSize not available - NoSuchMethodException");
          }  
          catch (InvocationTargetException e) 
          {
            Log.w(mTAG, "getSize not available - InvocationTargetException");
          } 
          catch (IllegalArgumentException e) 
          {
            Log.w(mTAG, "getSize not available - IllegalArgumentException");
          } 
          catch (IllegalAccessException e) 
          {
            Log.w(mTAG, "getSize not available - IllegalAccessException");
          }
      
          if (!sizeFound)
          {
            Log.i(mTAG, "Used deprecated methods as getSize not available");
            outSize = new Point(xiDisplay.getWidth(), xiDisplay.getHeight());
          }
      
          return outSize;
        }
      

      【讨论】:

        【解决方案4】:

        根据https://stackoverflow.com/a/1016941/2914140:

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        

        【讨论】:

          【解决方案5】:

          Display的新功能getSize()有什么问题?将 Point 对象转换为您需要的宽度和高度值真的很容易。

          【讨论】:

          • getSize() 方法是否仅适用于 api lvl 13 及更高版本,还是我弄错了?
          • 我们可以使用 widthPixels 来获取以下信息:“显示的绝对宽度,以像素为单位。” stackoverflow.com/a/37391867/1815624
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-17
          • 1970-01-01
          • 2011-06-15
          相关资源
          最近更新 更多