获取虚拟按键、状态栏等高度;

/获取屏幕原始尺寸高度,包括虚拟功能键高度
public static int getDpi(Context context){
    int dpi = 0;
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    @SuppressWarnings("rawtypes")
    Class c;
    try {
        c = Class.forName("android.view.Display");
        @SuppressWarnings("unchecked")
        Method method = c.getMethod("getRealMetrics",DisplayMetrics.class);
        method.invoke(display, displayMetrics);
        dpi=displayMetrics.heightPixels;
    }catch(Exception e){
        e.printStackTrace();
    }
    return dpi;
}

/**
 * 获取 虚拟按键的高度
 * @param context
 * @return
 */
public static  int getBottomStatusHeight(Context context){
    int totalHeight = getDpi(context);
    
    int contentHeight = getScreenHeight(context);
    
    return totalHeight  - contentHeight;
}

/**
 * 标题栏高度
 * @return
 */
public static int getTitleHeight(Activity activity){
    return  activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
}
/**
 * 获得状态栏的高度 
 *
 * @param context
 * @return
 */
public static int getStatusHeight(Context context)
{

    int statusHeight = -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());
        statusHeight = context.getResources().getDimensionPixelSize(height);
    } catch (Exception e)
    {
        e.printStackTrace();
    }
    return statusHeight;
}


/** 
 * 获得屏幕高度
 *  
 * @param context 
 * @return 
 */ 
public static int getScreenHeight(Context context)  
{   
    WindowManager wm = (WindowManager) context  
            .getSystemService(Context.WINDOW_SERVICE);  
    DisplayMetrics outMetrics = new DisplayMetrics();  
    wm.getDefaultDisplay().getMetrics(outMetrics);  
    return outMetrics.heightPixels;  
}
posted @ 2017-05-25 18:57  一点点征服  阅读(2037)  评论(0编辑  收藏  举报