/**
* 获取屏幕的宽
*
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
}
/**
* 获取屏幕的高度
*
* @param context
* @return
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
}
public static int getRealHeight(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenHeight = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics dm = new DisplayMetrics();
display.getRealMetrics(dm);
screenHeight = dm.heightPixels;
//或者也可以使用getRealSize方法
// Point size = new Point();
// display.getRealSize(size);
// screenHeight = size.y;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
try {
screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (Exception e) {
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
screenHeight = dm.heightPixels;
}
}
return screenHeight;
}
public static int getNavigationBarHeight(Context context) {
int navigationBarHeight = -1;
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
if (resourceId > 0) {
navigationBarHeight = resources.getDimensionPixelSize(resourceId);
}
return navigationBarHeight;
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public void getStatusBarHeightByReflect() {
int statusBarHeight2 = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusBarHeight2 = getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
Log.e(TAG, "状态栏高度-反射方式:" + statusBarHeight2);
}
/**
* 应用区的顶端位置即状态栏的高度
* *注意*该方法不能在初始化的时候用
* */
public void getStatusBarHeightByTop() {
Rect rectangle = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
Log.e(TAG, "状态栏高度-应用区顶部:" + rectangle.top);
}
/**
* 不能在 onCreate 方法中使用。
* 因为这种方法依赖于WMS(窗口管理服务的回调)。正是因为窗口回调机制,所以在Activity初始化时执行此方法得到的高度是0。
* 这个方法推荐在回调方法onWindowFocusChanged()中执行,才能得到预期结果。
*/
public void getAppViewHeight(){
//屏幕
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//应用区域
Rect outRect1 = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
int statusBar = dm.heightPixels - outRect1.height(); //状态栏高度=屏幕高度-应用区域高度
Log.e(TAG, "应用区高度:" + statusBar);
}
public static int getContentViewHeight(Activity activity) {
Rect rectangle= new Rect();
activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rectangle);
return rectangle.height();
}
public static void getTitleBarHeight(Activity activity) {
Rect outRect1 = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
int viewTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); //要用这种方法
int titleBarH = viewTop - outRect1.top;
Log.e(TAG, "标题栏高度-计算:" + titleBarH);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有