Android screen adaptation

Source: Internet
Author: User

first, some basic concepts1, Length (true length): inches, inch
2. Resolution: density pixels per inch dpi (density)
3, Pixels: px
4, Dip formula: px/dip=dpi/160 so dip similar to inches, length (dp=dip,sp similar to dip) dip=160*inch
dip= 160/dpi * px
When the dip is certain, the larger the DPI, the greater the PX
5, generalized resolution = long px* width px
second, the common law1, usually we say that the resolution of the cell phone is 320*480, in fact, the resolution here is the relative resolution
This means: The number of pixels in the horizontal direction is 320, and the number of pixels in the vertical direction is 480,
The resolution is 160 (the default is 160, which means the number of pixels per inch is 160)
So the horizontal direction: 320/160=2 inches
Vertical direction: 480/160=3 inches
The diagonal of the screen is the square root 13=3.6 (this is often said 3.6-inch screen)
2, said a mobile phone screen parameters have three: the product of long and wide pixels (relative resolution), real Resolution (DPI), diagonal length
3, the resolution of the simulator is 160, so the larger the pixel, the larger the screen
4, L, M, h three folders are based on true resolution DPI to find files.
third, solve the screen adaptation1. Pixel =x/user device pixels compared to y/development screen pixels
2. Dip by length (assuming the screen size is basically unchanged)
3, according to the density in the L, M, h folder (assuming that the screen size is basically unchanged, the larger the DPI is larger px)
If the phone is hdpi, but there is nothing in the hdpi, there is something in L, the program will go to L find the picture and enlarge it proportionally.
4. Individual adaptation
Screen resolution: 1024x600
Density:1 (160)
Folder: values-mdpi-1024x600
Screen resolution: 1024x600
density:1.5 (240)
Folder: values-hdpi-683x400 by 1024/1.5 600/1.5, need to be rounded.
Screen resolution: 800x480
Density:1 (160)
Folder: values-mdpi-800x480
Screen resolution: 800x480
density:1.5 (240)
Folder: values-hdpi-533x320 by 800/1.5 480/1.5, need to be rounded.
And so on
In general, you need to create values, VALUES-MDPI, and values-hdpi folders in case you can't find resources on a screen with no specified size.
four, type parameters:

ldpi Resources for low-density (ldpi) screens (~120DPI).
MDPI Resources for Medium-density (MDPI) screens (~160DPI). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240DPI).
xhdpi Resources for extra high-density (xhdpi) screens (~320DPI).

----------------------------------------------------------------
Screen Resolution: 720 * 1280
Absolute width: 720pixels
Absolute height: 1280pixels
Logic density: 2.0
X-dimensional: 294.96774 pixels per foot
Y dimension: 285.193 pixels per foot
The file under Res/drawable will do distortion compression
No processing of files under res/drawable-nodpi
v. Ways to get screen pixelsString str = "";
Displaymetrics dm = new Displaymetrics ();
DM = This.getapplicationcontext (). Getresources (). Getdisplaymetrics ();
int screenwidth = Dm.widthpixels;
int screenheight = Dm.heightpixels;
float density = dm.density;
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
str + = "screen Resolution:" + Dm.widthpixels + "*" + dm.heightpixels + "\ n";
str + = "absolute width:" + string.valueof (screenwidth) + "pixels\n";
str + = "absolute Height:" + string.valueof (screenheight)
+ "pixels\n";
str + = "logic density:" + string.valueof (density)
+ "\ n";
str + = "X-Dimension:" + string.valueof (xdpi) + "pixels per foot \ n";
str + = "Y dimension:" + string.valueof (ydpi) + "pixels per foot \ n";
The PPI is calculated as:
PPI =√ (length pixels 2 + width pixel number 2)/Screen diagonal inches
Dp:density-independent pixels, with 160PPI screen as standard, then 1dp=1px,
Conversion formulas for DP and PX:
dp*ppi/160 = px. such as 1DP x 320ppi/160 = 2px.
Sp:scale-independent pixels, which is the font unit for Android, is the standard 160PPI screen, when the font size is 100%, 1sp=1px.
Conversion formula for SP and px: sp*ppi/160 = px
It concludes that:
PX = dp*ppi/160
DP = PX/(ppi/160)
PX = sp*ppi/160
SP = px/(ppi/160)

public class Densityutil {    /**     * Turns from DP units to px (pixels) */public static int dip2px based on the resolution of the phone    (context context, Float dpvalue) {        final float scale = context.getresources (). Getdisplaymetrics (). density;        return (int) (Dpvalue * scale + 0.5f);    }    /**   * According to the resolution of the phone from px (pixels) to the unit to be DP */public    static int Px2dip (context context, float Pxvalue) {        final fl Oat scale = Context.getresources (). Getdisplaymetrics (). density;        return (int) (Pxvalue/scale + 0.5f);}    }






Welcome to scan QR Code, follow the public number



Android screen adaptation

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.