广

android开发

  • IOS开发
  • android开发
  • PHP编程
  • JavaScript
  • ASP.NET
  • ASP编程
  • JSP编程
  • Java编程
  • 易语言
  • Ruby编程
  • Perl编程
  • AJAX
  • 正则表达式
  • C语言
  • 编程开发

    Android编程实现获得手机屏幕真实宽高的方法

    2018-04-25 21:46:19 次阅读 稿源:互联网
    广告

    本文实例讲述了Android编程实现获得手机屏幕真实宽高的方法。分享给大家供大家参考,具体如下:

    WindowManager w = activity.getWindowManager();Display d = w.getDefaultDisplay();DisplayMetrics metrics = new DisplayMetrics();d.getMetrics(metrics);// since SDK_INT = 1;widthPixels = metrics.widthPixels;heightPixels = metrics.heightPixels;try {  // used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)  widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);  heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);} catch (Exception ignored) {}try {  // used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)  Point realSize = new Point();  Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);  widthPixels = realSize.x;  heightPixels = realSize.y;} catch (Exception ignored) {}

    补:改进版 (弥补了原先非支持版本的一些异常):

    WindowManager w = activity.getWindowManager();Display d = w.getDefaultDisplay();DisplayMetrics metrics = new DisplayMetrics();d.getMetrics(metrics);// since SDK_INT = 1;widthPixels = metrics.widthPixels;heightPixels = metrics.heightPixels;// includes window decorations (statusbar bar/menu bar)if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)try {  widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);  heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);} catch (Exception ignored) {}// includes window decorations (statusbar bar/menu bar)if (Build.VERSION.SDK_INT >= 17)try {  Point realSize = new Point();  Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);  widthPixels = realSize.x;  heightPixels = realSize.y;} catch (Exception ignored) {}

    希望本文所述对大家Android程序设计有所帮助。

    一起学吧部分文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与一起学吧进行文章共享合作。

    广告
    广告
    广告