在任意类中获得屏幕的宽高

来源:互联网 发布:android模拟器linux版 编辑:程序博客网 时间:2024/05/21 07:56
/** 获取手机的屏幕宽 */public static int getSW(Context context) {     int sw;     WindowManager wm = (WindowManager) context                .getSystemService(Context. WINDOW_SERVICE);     if (context.getResources().getConfiguration(). orientation == Configuration.ORIENTATION_PORTRAIT ) {           sw = wm.getDefaultDisplay(). getWidth(); //  当屏幕是竖屏     } else {            //sw = wm.getDefaultDisplay().getHeight();           sw = wm.getDefaultDisplay(). getWidth(); //  当屏幕是横屏     }     return sw;}/** 获取手机的屏幕高 */public static int getSH(Context context) {     int sh;     WindowManager wm = (WindowManager) context                .getSystemService(Context. WINDOW_SERVICE);     if (context.getResources().getConfiguration(). orientation == Configuration.ORIENTATION_PORTRAIT ) {           sh = wm. getDefaultDisplay().getHeight(); // 当屏幕是竖屏     } else {           sh = wm.getDefaultDisplay(). getHeight(); //  当屏幕是横屏            //sh = wm.getDefaultDisplay().getWidth();     }     return sh;}

Log日志:
这里写图片描述

0 0
原创粉丝点击