Get the length and width of the screen in Android

Source: Internet
Author: User

Android to get the length and width of the screen, referring to the internet there are a lot of code, but the results are not practical, such as my phone is i9000, screen size is 480*800px, the result is 320*533

The result is very unreliable, so I wrote a few lines of code, a close test

Test parameters:

Test environment: i9000 (Samsung)

Physical screen: 480*800px

density:1.5

Test code:

//Get screen density (method 1) int screenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();      Screen width (pixels, such as: 480px) int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); High screen (pixel, such as: 800p) log.e (TAG + "Getdefaultdisplay", "screenwidth=" + ScreenWidth +;


screenheight= "+ screenheight);
Get screen density (method 2) displaymetrics DM = new Displaymetrics ();

DM = Getresources (). Getdisplaymetrics ();        float density = dm.density;     Screen density (pixel scale: 0.75/1.0/1.5/2.0) int densitydpi = dm.densitydpi;
Screen density (pixels per inch: 120/160/240/320) Float xdpi = dm.xdpi;

float ydpi = dm.ydpi; LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + ";
Ydpi= "+ ydpi); LOG.E (TAG + "Displaymetrics", "density=" + density + ";

Densitydpi= "+ densitydpi);      ScreenWidth = Dm.widthpixels;     Screen width (pixels, such as: 480px) screenheight = Dm.heightpixels; High screen (pixel, such as: 800px) log.e (TAG + "Displaymetrics", "screenwidth=" + ScreenWidth + ";



screenheight= "+ screenheight); Get screen density (method 3) DM = new DisplaYmetrics ();

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);      density = dm.density;     Screen density (pixel scale: 0.75/1.0/1.5/2.0) densitydpi = dm.densitydpi;
Screen density (pixels per inch: 120/160/240/320) xdpi = dm.xdpi;

ydpi = dm.ydpi; LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + ";
Ydpi= "+ ydpi); LOG.E (TAG + "Displaymetrics", "density=" + density + ";

Densitydpi= "+ densitydpi);        int screenwidthdip = Dm.widthpixels;      Screen width (dip, such as: 320dip) int screenheightdip = Dm.heightpixels; Screen width (dip, such as: 533dip) log.e (TAG + "displaymetrics (222)", "screenwidthdip=" + Screenwidthdip + "; 

screenheightdip= "+ Screenheightdip);      ScreenWidth = (int) (Dm.widthpixels * density + 0.5f);     Screen width (px, such as: 480px) screenheight = (int) (Dm.heightpixels * density + 0.5f); High screen (PX, such as: 800px) log.e (TAG + "displaymetrics (222)", "screenwidth=" + ScreenWidth + "; screenheight= "+ screenheight); 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.