码迷,mamicode.com
首页 > 移动开发 > 详细

Android 获取屏幕宽高值

时间:2016-04-20 14:59:49      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

时间:2016年3月7日16:14:52

note:单位为像素。

三个方法中都是根据Display来进行测量。

  1. //方法一:
  2. WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  3. Display display = wm.getDefaultDisplay();
  4. int width = display.getWidth();
  5. int height = display.getHeight();
  6. System.out.println("width = " + width + " , height = " + height);
  1. //方法二:也可以获取到屏幕的密度density
  2. DisplayMetrics metrics = new DisplayMetrics();
  3. ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
  4. int widthPixels = metrics.widthPixels;
  5. int heightPixels = metrics.heightPixels;
  6. System.out.println("width = " + widthPixels + " , height = " + heightPixels);
  1. //方法三:
  2. WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
  3. Display display = wm.getDefaultDisplay();
  4. Point point = new Point();
  5. display.getSize(point);
  6. int width = point.x;
  7. int height = point.y;
  8. System.out.println("width = " + width + " , height = " + height);





Android 获取屏幕宽高值

标签:

原文地址:http://www.cnblogs.com/yuzhongzheng/p/5412575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!