Android-developed image resolution Solution

Source: Internet
Author: User

What is dpi?

Dpi is the abbreviation of "dot per inch". The number of dots per inch.
Four density classifications: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)
Normal screen: ldpi is 120, mdpi is 160, hdpi is 240, and xhdpi is 320.

Dpi Calculation Formula
DPI = pixel value/size of the diagonal line

Cell phone screen resolution and screen density are two things! If the resolution is not 800*480, the mobile phone image should be placed in the hdpi folder. 5.0 inch 800*480 belongs to mdpi

You can also get it by code:

 
 
  1. DisplayMetrics metric = new DisplayMetrics ();
  2. GetWindowManager (). getDefaultDisplay (). getMetrics (metric );
  3. Int width = metric. widthPixels; // screen width pixel)
  4. Int height = metric. heightPixels; // screen height pixel)
  5. Float density = metric. density; // screen density: 0.75/1.0/1.5)
  6. Int densityDpi = metric. densityDpi; // screen density DPI120/160/240)

Android will automatically load the corresponding resources based on the size and density of the screen, and convert them from the logical pixel DIP, used to define the interface layout) to the physical pixel on the screen.

First, let's see how the system uses the getDrawable (int id) method of Resources to find the image.

 
 
  1. public Drawable getDrawable(int id) throws NotFoundException { 
  2.         TypedValue value; 
  3.         synchronized (mAccessLock) { 
  4.             value = mTmpValue; 
  5.             if (value == null) { 
  6.                 value = new TypedValue(); 
  7.             } else { 
  8.                 mTmpValue = null; 
  9.             } 
  10.             getValue(id, value, true); 
  11.         } 
  12.         Drawable res = loadDrawable(value, id); 
  13.         synchronized (mAccessLock) { 
  14.             if (mTmpValue == null) { 
  15.                 mTmpValue = value; 
  16.             } 
  17.         } 
  18.         return res; 
  19.     } 
  20.   

TypedValue can be understood as the data storage type, which is mainly used by Resouces for the resource value held.
You can use the getValue (id, value, true) method to obtain the resource attributes of this id.

 
 
  1. public void getValue(int id, TypedValue outValue, boolean resolveRefs) 
  2.             throws NotFoundException { 
  3.         boolean found = mAssets.getResourceValue(id, 0, outValue, resolveRefs); 
  4.         if (found) { 
  5.             return; 
  6.         } 
  7.         throw new NotFoundException("Resource ID #0x" 
  8.                                     + Integer.toHexString(id)); 
  9.     } 
  10.   

Finally, the drawable is obtained through loadDrawable (value, id). This method reaches the underlying C code, which roughly means that the desired attribute value can be obtained through the methods and attributes in the TypedValue, and then the image is loaded.

OK. Here is an experiment.

Test 1: the mobile phone number is 1280*720 4.3 inch. The resolution of the xdpi image is 960*640. If the mobile phone number is set to, the image will not be filled with the entire phone number.

Folder loading time (MS) Image Display instructions

Drawable 311 Full Screen Picture stretched

Drawable-nodpi 130 unfilled screen image display normal

Drawable-ldpi 442 full screen image stretched

Drawable-mdpi 383 full screen image stretched

Drawable-hdpi 226 full screen image stretched

Drawable-xhdpi 109 unfilled screen image display normal

Test 2: the mobile phone number is 800*480 4.3 inch. The hdpi image resolution is 960*640. The normal image is not scaled. The picture should be filled with the entire mobile phone.) I put the picture in different drawable folders.

Folder loading time (MS) Image Display instructions

Drawable 290 full screen image stretch

Drawable-nodpi 127 full screen image display normal

Drawable-ldpi 369 full screen image stretch

Drawable-mdpi 346 full screen image stretch

Drawable-hdpi 124 full screen image display normal

Drawable-xhdpi 241 unfilled screen image scaling

Conclusion:

Images in drawable-nodpi are not stretched.

When the system obtains an image, it first goes to the dpi folder corresponding to the device to find the resource file. After finding the image, it should not scale and directly return the image.

If it is not found in the corresponding dpi folder, go back to the other folders to find it, and then scale it accordingly.

Images found at high dpi are scaled and stretched at low dpi.

In the source code, we can see that the loadDrawable process is actually implemented by layer C. Loading images by system resource id is more convenient than loading images directly at the java layer.

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.