Android DisplayMetrics returns incorrect screen size in pixels on ICS

前端 未结 6 1082
花落未央
花落未央 2020-11-27 03:24

I\'ve tried this....

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int fullscreenheight = metric         


        
6条回答
  •  轮回少年
    2020-11-27 03:49

    I found this hidden treasure for ICS ONLY......if you're targeting API's higher than ICS see the comment by Chris Schmich below.

    How to hide and display the navigation bar on Android ICS build

    In case the link dies....here's how to get the actual device screen size.....

    Display display = getWindowManager().getDefaultDisplay();     
    Method mGetRawH = Display.class.getMethod("getRawHeight");
    Method mGetRawW = Display.class.getMethod("getRawWidth");
    int rawWidth = (Integer) mGetRawW.invoke(display);
    int rawHeight = (Integer) mGetRawH.invoke(display);
    

    EDIT (11/19/12)

    The above code WILL NOT WORK on Android 4.2 and an exception will be thrown....I have yet to find a way to get the full screen size of a device in Android 4.2. When I do, I will edit this answer, but for now, you should code with contingency for android 4.2!!

提交回复
热议问题