How to get screen resolution in Android Honeycomb?

后端 未结 10 1848
情深已故
情深已故 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.

提交回复
热议问题