Android: Get the screen resolution / pixels as integer values

前端 未结 5 1477
再見小時候
再見小時候 2020-12-05 08:38

this is a really simple question on which I\'ve found no answer :/ How can I quickly access the screen resolution (width, height) as integer values?

I\'ve tried this

相关标签:
5条回答
  • 2020-12-05 09:15

    The trashkalmar answer is correct.

    However, the results will be specific to the activity context. If you want the whole device screen resolution:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
    int screenHeight = displayMetrics.heightPixels;
    

    Note that the results also depend on the current device orientation.

    0 讨论(0)
  • 2020-12-05 09:15

    I use the following:

    int scrWidth  = getWindowManager().getDefaultDisplay().getWidth();
    int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
    

    No muss, no fuss, just 2 class variables

    0 讨论(0)
  • 2020-12-05 09:25

    You can get screen metrics in this way:

    Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int width = d.getWidth();
    int height = d.getHeight();
    
    0 讨论(0)
  • 2020-12-05 09:28

    1.Simply use This inside activity to get screen width and height pixels.

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay()
        .getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    

    2.This can also be used But requires Api level inlined check

    Display display = getWindowManager().getDefaultDisplay();
    
    Point size = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(size);
        int width2 = size.x;
        int height2 = size.y;
    
    } else {
        int width2 = display.getWidth();
        int height2 = display.getHeight();
    }
    

    3.Use This when you are not in activity but having context

    WindowManager wm = (WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE);
    Display display1 = wm.getDefaultDisplay();
    Point size1 = new Point();
    
    int width1;
    int height1;
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    
        display1.getSize(size1);
        width1 = size1.x;
        height1 = size1.y;
    
    } else {
        width2 = display.getWidth();
        height2 = display.getHeight();
    }
    
    0 讨论(0)
  • 2020-12-05 09:29

    The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.

    0 讨论(0)
提交回复
热议问题