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、實際可用區(qū)域的寬高
/**
* 得到設(shè)備屏幕(可用區(qū))的寬度
*
* @param context 上下文
* @return int
*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 得到設(shè)備屏幕(可用區(qū))的高度
*
* @param context 上下文
* @return int
*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
3薇缅、狀態(tài)欄及底部導航欄的高度
/**
* 獲取狀態(tài)欄高度
*
* @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;
}