美文网首页
Android 设备屏幕数据

Android 设备屏幕数据

作者: 小相柳 | 来源:发表于2017-12-13 15:08 被阅读25次

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;
}

相关文章

网友评论

      本文标题:Android 设备屏幕数据

      本文链接:https://www.haomeiwen.com/subject/ztaaixtx.html