Android获取屏幕尺寸被弃用了吗?

18

嗨,我需要获取应用程序中屏幕的宽度。该应用程序将在2.1及以上版本上运行。我已经像下面这样设置了它。该方法已经被弃用,我应该使用getSize或其他方法。但问题是:这个方法在Android 3.0+和4.0+等版本上是否可行,还是会导致应用程序崩溃?我以前在线程中使用了一个已弃用的方法,结果导致冰淇淋设备上的应用程序崩溃。下面的方法是否可行?

 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;
5个回答

26

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

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;
}

12

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

但是,为了最大兼容性,这里最安全的方法是尝试使用反射来调用一个方法,如果失败则退而求其次。实际上,您可以创建自己的 getSize() 方法,以确保不会失败。目前我无法测试,但它可能看起来像这样:

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()只需要一个参数。这有帮助吗? - Steve Blackwell
1
或者您可以使用未过时的Display getMetrics() - Johan vdH
1
@JohanvdH - 你说得对。getMetrics()自API 1以来就存在,看起来返回的值是相同的。这可能是最简单的方法。 - Steve Blackwell

3
根据https://dev59.com/hnNA5IYBdhLWcg3wUL5Y#1016941的说法:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;

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;
  }

1

Display 的新功能 getSize() 有什么问题吗?将 Point 对象转换为所需的宽度和高度值非常容易。


2
getSize() 方法不是只有在 API 等级 13 及以上才可用吗?还是我弄错了? - Ukjent
我们可以使用widthPixels来获取以下信息:“显示器的绝对像素宽度。” https://dev59.com/_Wgv5IYBdhLWcg3wW_h_#37391867 - CrandellWS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接