Android 设备屏幕数据

1、分辨率
/**
* 获取屏幕分辨率的宽
*
* @param context
* @return
*/
public static int getScreenResolutionWidth(Context context) {
Point screenPoint = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
((Activity) context).getWindowManager().getDefaultDisplay().getRealSize(screenPoint);
} else {
((Activity) context).getWindowManager().getDefaultDisplay().getSize(screenPoint);
}
return screenPoint.x;

}

/**
 * 获取屏幕分辨率的高
 *
 * @param context
 * @return
 */
public static int getScreenResolutionHeight(Context context) {
    Point screenPoint = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        ((Activity) context).getWindowManager().getDefaultDisplay().getRealSize(screenPoint);
    } else {
        ((Activity) context).getWindowManager().getDefaultDisplay().getSize(screenPoint);
    }
    return screenPoint.y;
}

2、实际可用区域的宽高
/**
* 得到设备屏幕(可用区)的宽度
*
* @param context 上下文
* @return int
*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}

/**
 * 得到设备屏幕(可用区)的高度
 *
 * @param context 上下文
 * @return int
 */
public static int getScreenHeight(Context context) {
    return context.getResources().getDisplayMetrics().heightPixels;
}

3、状态栏及底部导航栏的高度
/**
* 获取状态栏高度
*
* @param context
* @return
*/
public static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object o = c.newInstance();
Field field = c.getField("status_bar_height");
int x = (Integer) field.get(o);
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}

/**
 * 获取底部导航栏高度
 *
 * @param context
 * @return
 */
public static int getNavigationBarHeight(Context context) {
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、拨打电话 public static void call(Context context, String ph...
    jxuanwu阅读 1,330评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,695评论 19 139
  • 主要积累一些开发中比较 常用的工具类,部分借鉴于网络,主要来源于平时开发因需求而生的小工具类 13、ArithUt...
    大鸭梨leepear阅读 736评论 0 1
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,384评论 0 17
  • 最近我去了一所帮助青少年戒网瘾的学校做一些心理辅导活动,一些孩子都有网络成瘾的问题,父母面对这样的困难无计可施,于...
    南京爱家心坊阅读 766评论 2 8

友情链接更多精彩内容