简体   繁体   English

如何以编程方式获得Nexus 7(flo)真实屏幕分辨率?

[英]How to get Nexus 7(flo) real screen resolution programmatically?

My device is Nexus 7(Flo) .I want to get the real screen resolution,which the height is 1920,But the code 我的设备是Nexus 7(Flo)。我想获取真正的屏幕分辨率,高度为1920,但是代码

DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
int   mPageHeight = displayMetrics.heightPixels;

returns 1824, which is the result of 1920 minus the NavigationBar's height(48dp). 返回1824,这是1920减去NavigationBar的高度(48dp)的结果。 I know in Immersive Mode the decorView's height is 1920.But I want to acquire the value in "onCreate" method in Activity. 我知道在沉浸式模式下decorView的高度为1920。但是我想在Activity中的“ onCreate”方法中获取值。 DecorView's height is assigned too late. DecorView的高度分配得太晚。

So how to get the real screen resolution height? 那么如何获得真实的屏幕分辨率高度呢?

Or is there a reliable way to know if the NavigationBar at bottom is showing ?You know most devices don't have that NavigationBar. 还是有一种可靠的方法来知道底部的NavigationBar是否显示?您知道大多数设备都没有该NavigationBar。

You can get the full screen resolution by using this code 您可以使用此代码获得全屏分辨率

View decorView = context.getActivity().getWindow().getDecorView();
Point outSize = new Point();

decorView.getDisplay().getRealSize(outSize);
Log.i("ImmersiveHeight : "+outSize.y);

set the immersie mode to your activity by using this code 使用此代码将沉浸模式设置为您的活动

final int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN 
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

decorView.setSystemUiVisibility(uiOptions);

This code takes effect! 该代码生效! I get 1920x1200 finally! 我终于得到1920x1200了!

 public void printHeight(){
    int width = 0, height = 0;
    final DisplayMetrics metrics = new DisplayMetrics();
    Display display = getWindowManager().getDefaultDisplay();
    Method mGetRawH = null, mGetRawW = null;

    try {
        // For JellyBean 4.2 (API 17) and onward
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealMetrics(metrics);

            width = metrics.widthPixels;
            height = metrics.heightPixels;
            Log.i("ZYStudio", "with and height:"+width+"|"+height);
        } else {
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");
            Log.i("ZYStudio", "rawW and rawH:"+mGetRawW+"|"+mGetRawH);

            try {
                width = (Integer) mGetRawW.invoke(display);
                height = (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();
            }
        }
    } catch (NoSuchMethodException e3) {
        e3.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM