Android resolution adaptation

Source: Internet
Author: User

Android's resolution adaptation issue has always been a major problem for Android, referring to the official development documentation and some of the techniques for dealing with resolution in the actual development to communicate with you.

Official document on resolution adaptation "Supportingmultiple Screens"

Overview of Screens Support
    • Screen Size: Display size
    • Screen density: Display density
    • Orientation: Direction
    • Resolution: Resolution
    • Density-independent pixel (DP or DIP): a concept similar to pixels, which is the unit of the smallest granularity that makes up a screen, equivalent to a physical pixel on a 160dpi screen.
Range of screens supported

Android supports multi-resolution adaptation from 1.6 (API level 4), dividing screen size and density:
* Screen Size: Small,normal,large,xlarge
* Screen density: ldpi (Low), MDPI (Medium), hdpi (High), xhdpi (Extra high)

The size and density benchmarks here are based on the first Android T-mobile G1 (HVGA screen), so in order to reduce the product's adaptation cost, it is only necessary to consider the suitability on generailized size/density.

In the design of the actual layout, should be used in the unit is DP, so you can avoid the screen due to different dpi, the most typical example is the retina screen of Apple.

    • Xlarge:at least 960DP x 720DP
    • Large:at least 640DP x 480DP
    • Normal:at least 470DP x 320DP
    • Small:at least 426DP x 320DP

PX = DP * (dpi/160)

For example, a 240dpi screen, 1DP is equivalent to 1.5 pixels

Px,dpi,dp,dip,sp tr>
Unit of Measure explanation
px (pixel pixels) pixel units
dpi (dots per inch pixel density) pixels per inch, assuming the device's resolution is 320 * 240, screen length 2 inches wide 1.5 inches, dpi=320/2=240/1.5=160
dp (density density) pixels per ordinary inch density=reso Lution/screen size
Dip (device-independent pixel, device independent pixels) is the same as DP, different devices have different display effects, this and the device hard We recommend this for support WVGA, HVGA, and QVGA, and do not rely on pixels. The corresponding formula for dip and specific pixel values is the dip value = device density/160* pixel value, which can be seen on devices with a dpi (pixel density) of 160dpi 1px=1dip
sp (scaledpixels magnified pixels) is primarily used for font display (best for TEXTSIZE). According to Google's suggestion, TextView's font size is best to use SP to do units, and view the source of TextView know that Android by default using the SP as the font size units.
Screen density DPI resolution Match grouping
QVGA density=0.75 densitydpi=120 QVGA (240 * 320) ldpi
HVGA density=1.0 densitydpi=160 HVGA (320 * 480) mdpi
Vga density=1.0 densitydpi=160 VGA (480 * 640) mdpi
WVGA density=1.5 densitydpi=240 WVGA (480 * 800) hdpi
WQVGA density=2.0 densitydpi=120 WQVGA (240*400) xhdpi
Why you should consider DPI when designing the UI

Here is an example, in the absence of DPI in the case of a 32px size button on the interface, then in the same 3.7-inch, the resolution is 480 * 320, 600 * 480, 800 * 600 of the mobile phone display results as follows:

As can be seen from the figure, because the device DPI increases, the corresponding physical length of the 1px on the device is reduced, the final display effect is that the component looks smaller.

Dimensioning units with DPI as the interface

Dimension the control to 32dip * 32dip, resulting in a display effect:

You can see that the effect of the display is much better. Android defaults to using density to match the resource file, not the screen size, while the image resolution and screen mismatch, the system to require a lot of extra memory to stretch and scale the image, these additional overhead, will reduce the efficiency of the program running. The most critical is the exception that can cause memory overflow, which is caused by the Android memory recovery mechanism, for a large number of picture use situation, the probability of occurrence is quite high, there is no effective solution.

Get density and dip

Mobile phone density and dip can be obtained using the following methods, two methods, one is Getwindowmanager acquisition, one is obtained through getresources, the two results are the same, but for some reason, there may be differences, some handset manufacturers will modify the API, Some manufacturers unscrupulous manufacturers will let the low-end screen intentionally display high-end data, if you get the screen data is not too accurate, do not care too much:

void getDefaultDisplayScreenSize(){    DisplayMetrics dm = newDisplayMetrics();    getWindowManager().getDefaultDisplay().getMetrics(dm);    int screenWidthDip = dm.widthPixels;// 屏幕宽(dip,如:320dip)    int screenHeightDip = dm.heightPixels;// 屏幕宽(dip,如:533dip)    float density= dm.density;// 屏幕密度(像素比例:0.75/1.0/1.5/2.0)    int densityDPI= dm.densityDpi;// 屏幕密度(每寸像素:120/160/240/320)    float xdpi= dm.xdpi;    float ydpi= dm.ydpi;    w= dm.widthPixels;// 屏幕宽(px,如:480px)    h= dm.heightPixels;// 屏幕高(px,如:800px)}void getDefaultDisplayScreenDensityDPI(){    DisplayMetricsdm = newDisplayMetrics();    getResources().getDisplayMetrics();    int screenWidthDip = dm.widthPixels;// 屏幕宽(dip,如:320dip)    int screenHeightDip = dm.heightPixels;// 屏幕宽(dip,如:533dip)    float density= dm.density;// 屏幕密度(像素比例:0.75/1.0/1.5/2.0)    int densityDPI= dm.densityDpi;// 屏幕密度(每寸像素:120/160/240/320)    float xdpi= dm.xdpi;    float ydpi= dm.ydpi;}
The process of screen adaptation

1. Declare the application compatible screen size
Used in the manifest file to declare an app-compatible screen size. This allows the device to determine whether to run the app using screen compatibility mode.

2. Provide different layout layouts for different screen sizes
The layout file opposite is placed under the Layout-small,layout-normal,layout-large,layout-xlarge folder.

3. Provide different picture resources for different dpi
If you use dip to mark the size of the component, when the picture is drawn, the pixel size of the picture resources will be scaled to fit the size of the screen components, it is easy to produce display blur, fillet deformation and other problems, so the different DPI to prepare different picture resources. If you have different picture resources for different DPI devices, the Android Reference document recommends that you do ldpi,mdpi,hdpi and xhdpi resources on the 3:4:6:8 scale, and that Android official reference document "Icondesign Guidelines "

Android resolution adaptation

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.