Android Development gets phone's screen width, density value, PX wide, DP wide, and PX and DP Mutual

Source: Internet
Author: User

Directly paste Code

Tvmodel.settext (Android.os.Build.MODEL)/Mobile phone model

Get screen density (Method 1)
int screenWidth1 = Getactivity (). Getwindowmanager (). Getdefaultdisplay (). GetWidth (); Screen width (pixels, such as: 480px)
int screenHeight1 = Getactivity (). Getwindowmanager (). Getdefaultdisplay (). GetHeight (); High screen (pixel, such as: 800p)

Tvscreenheight1.settext (screenHeight1 + "px");
Tvscreenwidth1.settext (screenWidth1 + "px");


Get screen density (Method 2)
Displaymetrics dm2 = Getresources (). Getdisplaymetrics ();

float density = dm2.density; Screen density (pixel scale: 0.75/1.0/1.5/2.0)
int densitydpi = dm2.densitydpi; Screen density (pixels per inch: 120/160/240/320)
float xdpi = dm2.xdpi;
float ydpi = dm2.ydpi;

int screenWidth2 = Dm2.widthpixels; Screen width (pixels, such as: 480px)
int screenHeight2 = Dm2.heightpixels; High screen (pixel, such as: 800px)

Tvscreenheight2.settext (screenHeight2 + "px");
Tvscreenwidth2.settext (screenWidth2 + "px");
Tvscreendensity2.settext (density + "");
Tvscreendensitydpi2.settext (densitydpi + "");

Get screen density (Method 3)
Displaymetrics dm3 = new Displaymetrics ();
Getactivity (). Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM3);

density = dm3.density; Screen density (pixel scale: 0.75/1.0/1.5/2.0)
densitydpi = dm3.densitydpi; Screen density (pixels per inch: 120/160/240/320)
xdpi = dm3.xdpi;
ydpi = dm3.ydpi;

Tvscreendensity3.settext (density + "");
Tvscreendensitydpi3.settext (densitydpi + "");

int screenWidth3 = Dm3.widthpixels; Screen width (px, such as: 480px)
int screenHeight3 = Dm3.heightpixels; High screen (PX, such as: 800px)

Tvscreenheight3.settext (screenHeight3 + "px");
Tvscreenwidth3.settext (screenWidth3 + "px");

float Screenwidthdip = (dm3.widthpixels/density); Screen width (dip, such as: 320dip)
float Screenheightdip = (dm3.heightpixels/density); Screen width (dip, such as: 533dip)

Tvscreenheight4.settext (Screenheightdip + "dip");
Tvscreenwidth4.settext (Screenwidthdip + "dip");

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.