Android screen suitable recipe method

Source: Internet
Author: User

Android picture adaptation is a tricky thing, this article detailed introduction screen display principle, why the resource file will exist

DRAWABLE-LDPI, drawable-mdpi, drawable-hdpi, drawable-xhdpi and other folders, why use DP as a unit and so on.

Let's start with the display of these two pictures.

    1. 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.

    2. 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.

    3. Finally, why use DP as the unit instead of PX in the layout file?

      DP is a non-screen PPI parameter and a virtual unit that does not actually exist. The display will be mapped according to the PPI of the specific screen, the relationship is as follows 160ppi 1DP 1px120ppi 1DP 0.75px240ppi
      1DP 1.5px320ppi 1DP 2px Therefore, in the layout file generally use DP as the unit, so that it can adapt to different PPI screen, on the various PPI screen has the same large physical distance. (It can be seen that 1DP is not always equal to 1px, for PPI high screen Ken can be equal to 2px, just to physically look at the distance is equal)

    4. Finally, I hope you can understand the resolution we often say and the relationship between the PPI, in fact, they are not much of a relationship. The resolution is just a description of how many pixels are on the screen, which is also related to the screen size. Of course, for the same screen size, the higher the resolution, the larger the PPI, and it doesn't make any sense to leave the physical dimensions of the screen to talk about resolution. It's like a flat plate with a larger resolution than a phone, because it's big. It is not necessarily better than a mobile phone to show the fineness of color.

Android screen suitable recipe method

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.