Android 是否弃用了屏幕尺寸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10659954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:31:20  来源:igfitidea点击:

Android get Screen Size deprecated?

androidsizedeprecated

提问by Ukjent

Hey I need to get the width of the screen in my application. The application will run on 2.1 and upwards. I have set it up like the one below. The method is deprecated and i should proabably use getSize or a other way. But the question is: Will this work on android versions like 3.0+ and 4.0+, or will it make the app crash. I have used a deprecated method in a thread before and it made the app crash on ice cream devices. Will the method below work ?

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

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

EDIT:

编辑:

I have tried the getSize but i dont get it to work:

我试过 getSize 但我没有让它工作:

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

采纳答案by Steve Blackwell

I don't know whether these deprecated methods will work on Android 3 and 4. The best way to tell is to test on an emulator.

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

But, the safest method here for max compatibility will be to try one method using reflection, and fall back to the other. Essentially, you could make your own version of getSize()that can't fail. I can't test this atm, but it might look like this:

但是,为了最大兼容性,这里最安全的方法是使用反射尝试一种方法,然后返回到另一种方法。从本质上讲,您可以制作自己的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();
    }
}

Then of course just call it with something like

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

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

回答by AndDev

I am not sure but this may work:

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

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

回答by Dan J

I've extended Steve's helpful code so that Eclipse doesn't give any warnings or errors, and I've also restructured it slightly. Since the Point class has been present since API level 1 I didn't see much benefit in creating it through reflection.

我已经扩展了 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;
  }

回答by CoolMind

According to https://stackoverflow.com/a/1016941/2914140:

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

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

回答by Turnsole

What is wrong with Display's new function, getSize()? It'd be really easy to turn the Point object into the width and height values you need.

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