This is a picture of 200x200 on a different phone display effect. Is it obvious that the second picture looks much smaller than the first one? What is this for?
Here we introduce the PPI (pixel per inch) noun, which translates to the number of pixels per inch of distance. Now to explain the above phenomenon. For a picture of 200x200 pixels, our country we want to display on the 200ppi mobile phone, then occupy the screen length and width are 200pix/(200pix/inch) = 1 inch, that is to occupy 1 inch x 1 inch, if you want to display in 300ppi mobile phone, then occupy the length and width of the screen is 200pix/(300pix/inch) = 2/3 inch, that is, the higher the PPI cell phone, the unit inch can display more pixels, so when the two phones display the same number of pixels, p The higher the Pi, the less space the phone occupies. This is the problem we need to solve when we want images to show consistent results on various phone screens.
Since this effect is not what we want, how can we get a consistent display effect? Here are three ways to
Method one: For each of the different PPI use a different picture, which requires us to determine the screen PPI, and then the image processing, and then display, obviously more troublesome. And for getting the screen PPI this parameter Android does not provide us with API calls. So this is basically not a viable option.
Method Two: After building an Android project with the Eclipse development environment, the system will generate DRAWABLE-LDPI, drawable-mdpi, drawable-hdpi, drawable-xhdpi, DRAWABLE-XXHDPI, for the same picture resource, we created these five versions separately to accommodate different screens. Suppose you want a picture
The length and width shown on the 200ppi and 400ppi phones are the same for 1inch x 1inch, so you need to make a picture of 200x200 and a
400x400 the picture, then put in the corresponding drawable directory will be able to. So how does the program run to know which drawable directory you're using?
Picture of it? This is not necessary for us to consider, but the handset manufacturers to consider the problem, just need to know that the program will be used to adapt to the current screen of the picture under the directory
Resources.
DRAWABLE-LDPI, drawable-mdpi, drawable-hdpi, drawable-xhdpi,drawable-xxhdpi, these directories correspond to the Pppi for
120ppi 160ppi 240ppi 320ppi 480ppi, that is, if the screen PPI is 320, then the DRAWABLE-XHDPI directory will be preferred to use the
Picture resources. Of course, the cell phone screen PPI is also a variety of, for example, some 300ppi, some 220ppi, the system will choose the closest directory, also
300ppi phone will choose 320ppi corresponding resources in the drawable directory
Method Three: Method 2 needs to generate a corresponding image version for each drawable directory, if the picture is more, generate different version of the picture is trivial the main disadvantage is to increase the size of the application, then can only be stored in a directory of pictures, For example, only in the DRAWABLE-MDPI directory to store the image of the answer is yes, assuming we have a drawable-hdpi (corresponding PPI 160ppi) in the directory to store a 100x100 picture, then when the picture needs
When displayed on a 320ppi Android device, the Android system will automatically process the image to create a 200x200 image to display on the device, then they occupy a space of 100/160 = 3/5 inch 200/320 = 3/5 in CH can see that they occupy the same length and width are the same the advantage of this autoscale is that only one picture can be adapted to the various PPI types of screens. The disadvantage is that when the image of the 100x100 to transform into a 200x200 image will be able to cause the image is not clear, if provided a drawable-xhdpi under the picture resources, the picture more words will be invisible increase the size of the application, so say how to choose is also a measure. The recommendation is to provide multiple versions of the more important picture resources (when the Android device and its own PPI correspond to the drawable of the desired resource, no more resources will be found in other directories). It's not important. The image resource stores a version that allows the system to display the appropriate scale of the image according to its own PPI.