How to get screen resolution in Android Honeycomb?

后端 未结 10 1811
情深已故
情深已故 2020-12-24 09:07

I want to get real resolution of screen on Android Honeycomb.

Here\'s my code

Display display = getWindowManager().getDefaultDisplay();
int w = displ         


        
相关标签:
10条回答
  • 2020-12-24 09:30

    This is another viable solution for measuring the screen size (API 11+):

    public static ABDimension calculateScreenDimensions(Activity activityGet)
    {
        ABDimension abdReturn = new ABDimension();
    
        try
        {
            Rect rectGet = new Rect();
            Window winGet = null;
            DisplayMetrics displayMetrics = new DisplayMetrics();
            int nStatusBarHeight = 0;
            int nContentViewTop = 0;
            int nTitleBarHeight = 0;
            int nScreenHeight = 0;
    
            if((activityGet != null) && (rectGet != null) && (displayMetrics != null))
            {
                winGet = activityGet.getWindow();
                if(winGet != null)
                {
                    winGet.getDecorView().getWindowVisibleDisplayFrame(rectGet);
                    nStatusBarHeight = rectGet.top;
                    nContentViewTop = winGet.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                    nTitleBarHeight = nContentViewTop - nStatusBarHeight;
    
                    activityGet.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                    int screenHeight = displayMetrics.heightPixels;
                    abdReturn.nWidth = displayMetrics.widthPixels;
                    abdReturn.nHeight = screenHeight - (nTitleBarHeight + nStatusBarHeight);
                }
            }
        }
        catch(Exception e)
        {
            appContext.showMessage(false,"Error","[calculateScreenDimensions]: "+e.toString());
        }
    
        return abdReturn;
    }
    

    Where the ABDimension class contains six integers labeled nWidth, nHeight, nMarginLeft, nMarginTop, nMarginRight and nMarginBottom. This version accounts for common Android decor components like the TitleBar/StatusBar.

    0 讨论(0)
  • 2020-12-24 09:39

    In the question Height of statusbar? explain how you get the statusbar height. Having the statusBarHeight value, you can do:

    Display display = getWindowManager().getDefaultDisplay();
    int h = display.getHeight() + statusBarHeight;
    
    0 讨论(0)
  • 2020-12-24 09:39

    Perhaps you can add the height of the status bar. Height of statusbar?

       Rect rectgle= new Rect();
       Window window= getWindow();
       window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
       int StatusBarHeight= rectgle.top;
       int contentViewTop= `enter code here`
          window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
       int TitleBarHeight= contentViewTop - StatusBarHeight;
    
       Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
    
    0 讨论(0)
  • 2020-12-24 09:47

    A function to get the screen size at API11 level minimum; also includes methods for higher level APIs (change function to use the selected API, its function call and its return object type):

    public int getScreenOrientation()
    {
        int nOrientation = Configuration.ORIENTATION_UNDEFINED;
    
        try
        {
            DisplayMetrics displayMetrics = null;
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if(windowManager != null)
            {
                Display defaultDisplay = windowManager.getDefaultDisplay();
                if(defaultDisplay != null)
                {
                    defaultDisplay.getMetrics(displayMetrics);//API11
                    //defaultDisplay.getRectSize(rectGet);//API13
                    //defaultDisplay.getSize(pointGet);//API13
                    //defaultDisplay.getCurrentSizeRange(pointGet,pointGet);//API16
                    //defaultDisplay.getRealSize(pointGet);//API17
                    //defaultDisplay.getRealMetrics(displayMetrics);//API17
                    if((displayMetrics.widthPixels == displayMetrics.heightPixels) || (displayMetrics.widthPixels < displayMetrics.heightPixels))
                    {
                        nOrientation = Configuration.ORIENTATION_PORTRAIT;
                    }
                    else
                    {
                        nOrientation = Configuration.ORIENTATION_LANDSCAPE;
                    }
                }
            }
        }
        catch(Exception e)
        {
            showMessage(false,"Error","[getScreenOrientation]: " + e.toString());
        }
    
        return nOrientation;
    }
    

    Note: Configuration.ORIENTATION_SQUARE is deprecated, so here it is replaced to default to ORIENTATION_PORTRAIT.

    0 讨论(0)
  • 2020-12-24 09:49

    Use the DisplayMetrics structure, describing general information about the display, such as its size, density, and font scaling.

    The code used to get the display's height is as follows:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    Log.d("log", "OOO " + metrics.heightPixels);
    

    It's supposed to return the absolute height of the display, in pixels.

    0 讨论(0)
  • 2020-12-24 09:50

    Here you are, with little trick and assumption that screen decorations are on top/bottom and never on left/right:

    Display d = getWindowManager().getDefaultDisplay();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    int h = d.getWidth();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    int w = d.getWidth();
    
    0 讨论(0)
提交回复
热议问题