Android DisplayMetrics returns incorrect screen size in pixels on ICS

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

I\'ve tried this....

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


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 04:03

    Try this

        final DisplayMetrics metrics = new DisplayMetrics(); 
        Display display = getWindowManager().getDefaultDisplay();     
        Method mGetRawH = null,mGetRawW = null;
    
        try {
                        // For JellyBeans and onward
            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
                display.getRealMetrics(metrics);
    
                realWidth = metrics.widthPixels;
                realHeight = metrics.heightPixels;
            }else{
                mGetRawH = Display.class.getMethod("getRawHeight");
                mGetRawW = Display.class.getMethod("getRawWidth");
    
                try {
                    realWidth = (Integer) mGetRawW.invoke(display);
                    realHeight = (Integer) mGetRawH.invoke(display);
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    

提交回复
热议问题