开发者

How can I get the exact screen size of the device through code?

I used the below code to find out the screen size but I am not getting correct values.

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int width = display.getWidth(); 
    int height=display.getHeight();

It returns 320 x 480 for galaxy tab.. which is not correct.

How do开发者_运维百科 I find the correct screen size of the galaxy tab which is 600 x 1024.


I always use this one, and never encountered an error:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();


This sounds like Android thinks that your application is a "legacy" app (i.e. one designed for old versions of Android). From Supporting Multiple Screens:

For instance, suppose a given device is using a WVGA medium density screen, classified as a "large" screen, but the application states that it does not support large screens; in this case, the system will again "lie" to the application when it queries for screen dimensions, and report 320x480.


public static int getScreenWidth(Context context){
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    return display.getWidth();
}
public static int getScreenHeight(Context context){
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    return display.getHeight();
}


in your program, width's unit is px, your said 600 is possible dp value,you can use the follow code to verify:

final float scale = context.getResources().getDisplayMetrics().density;

prite scale to see, if scale = 320/600 = 0.5333333, that is to say 600 is dp value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜