How to get the screen size of the device?

前端 未结 4 1612
抹茶落季
抹茶落季 2020-12-09 00:32

The desire have 480 x 800 pixels, 3.7 inches and the HD have 480 x 800 pixels, 4.3 inches screen specification.

I run the code that is accepted as answer from this t

4条回答
  •  粉色の甜心
    2020-12-09 00:49

    OK FINALLY I found what is my problem

    Yes this code works fine but I doesn't return pixels, It returns DIP (Density independent pixels ) which is not the same !

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels; //320 dip
    int height = dm.heightPixels; //533 dip
    

    What I needed is the REAL pixels and the calculation for them is like this:

    int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));
    

    now the widthPix is 480 !

提交回复
热议问题