How Android gets the screen hardware resolution

Source: Internet
Author: User

Textview TV = (textview) findviewbyid (R. Id. TV );

Displaymetrics dm = new displaymetrics ();

Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );

// The resolution is only an integer.

Int width = math. Round (DM. widthpixels * DM. Density );

Int Height = math. Round (DM. heightpixels * DM. Density); // The value is calculated as 799.5 and rounded to an integer of 800 using math. Round.

TV. settext (width + "X" + height );

DM. withpixels and DM. the width and height values obtained by heightpixels are both based on the width of 320. That is to say, no matter what resolution your screen is, the width of the screen will be 320, and the height is calculated based on the aspect ratio,

Therefore, the width and height obtained on the milestone (480X854) is 320x569. To obtain a real resolution, multiply the value by DM. density,

This value varies with the hardware resolution. If the hardware width of the screen is 320 pixels, this value is 1,

If it is 480, the value is 480/320, Which is 1.5.

 

Next we will compare the differences among dip, DP, PX, and SP in Android:

Dip: Device Independent pixels (device independent pixel ). different devices have different display effects, which are related to the hardware of the device. We recommend that you use this function to support WVGA, hvga, and qvga without pixels.
Px: pixels (pixels). Different devices have the same display effect. Generally, we use hvga to represent 320x480 pixels, which is usually used.
PT: point, a standard unit of length, 1pt = 1/72 inch, used in the printing industry, very easy to use;
SP: scaled pixels (zoom in pixels). It is mainly used to display the best for textsize in fonts. According to Google's suggestion, it is best to use SP as the unit for textview font size,

Past,ProgramUsers usually design the computer user interface in pixels. For example, define a form field with a width of 300 pixels. the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem with this processing is that if you run the program on a new display with a dot (DPI) higher per inch, the user interface will look small. In some cases, the user interface may be small to difficult to see the content.
Measurement Units irrelevant to resolution can solve this problem. Android supports all of the following units:
Px (pixel): the point on the screen.
In (INCHES): the unit of length.
Mm (mm): the unit of length.
Pt (lbs): 1/72 inch.
DP (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
Dip: it is the same as DP and is mostly used in Android/ophone examples.
SP (pixels irrelevant to the scale): similar to DP, but can be scaled based on the user's font size preferences.

 

 

Note: according to Google's recommendation, dip is used for pixels and SP is used for fonts. For example, the difference between PX and dip is that PX is a pixel. If PX is used, the actual pixel will be used. For example, draw a horizontal line with a length of PX, in a 480-width simulator, the screen width is half the screen width, and in a 320-width simulator, the screen width is 2/3. DIP is to divide the screen's high score into 480 points and the width into 320 points. For example, if you build a 320 dip horizontal line, whether you are on a 480 or simulator, it is half the screen length. Public static int dip2px (context, float dipvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (dipvalue * scale + 0.5f );
}

Public static int px2dip (context, float pxvalue ){
Final float scale = context. getresources (). getdisplaymetrics (). density;
Return (INT) (pxvalue/scale + 0.5f );
}
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.