获得屏幕宽高

来源:互联网 发布:java 静态变量初始化 编辑:程序博客网 时间:2024/04/30 13:59
public static int getScreenHeight(Context context) {int heightPixels;WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display d = wm.getDefaultDisplay();DisplayMetrics metrics = new DisplayMetrics();d.getMetrics(metrics);heightPixels = metrics.heightPixels;if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {try {heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);} catch (Exception e) {e.printStackTrace();MLog.i("", "Error >= 14 ,< 17 " + e.getMessage());}} else if (Build.VERSION.SDK_INT >= 17) {android.graphics.Point realSizePoint = new Point();try {Display.class.getMethod("getRealSize", android.graphics.Point.class).invoke(d, realSizePoint);heightPixels = realSizePoint.y;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();MLog.i("", "Error >= 17 " + e.getMessage());}}MLog.i("", "real size = " + heightPixels);return heightPixels;}

0 0
原创粉丝点击