Android: The difference between different drawable folders

Source: Internet
Author: User

After 4.0, a new Android project will automatically generate drawable,drawalbe-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxhdpi six folders, In addition to Drawable, the other 5 folders correspond to four levels of Density:120dip (low), 160dip (Medium), 240dip (High), 320dip (Xhigh), 480dip (drawable-xxhdpi). The current mainstream approach is to put the picture file in the Drawable-hdpi folder, and the image-related XML file (such as button XML, to display a different background image effect when the button is clicked) in the Drawable folder.

The so-called density refers to the pixel density on the screen, with 160dip as the standard density. For example, when we place a view in the layout file Layout.xml

  <View            android:layout_height="1dip"           android:layout_width ="100dip"/>

The view shows a length of 100px (pixels) long on a screen with a density of 160dip, and its length will be 200px on a 320dip screen.

This conversion is also required when the picture is in the Drawable folder. If there is a picture of 22*44 in the drawable-mdpi folder, when the program reads it into the system, assuming that the screen density is 320dip, then it will be 44*88 in memory size.

Previously said, the current mainstream practice is to put the picture in the Drawable-hdpi folder, the folder corresponding to the density is 240dip, but in fact, now the phone is generally 320dip or even 480dip, So the images placed in the hdpi folder are automatically stretched when displayed.

Another question is, what is the size of the image when I read the image directly from the SD card or assert folder? When we read the image inside the SD card, we first get not the Drawbale object but the bitmap object, the width and height of the bitmap object correspond to the true pixel size of the picture. When converting a bitmap object to drawable, we do not scale unless we manually set the target density.

New Bitmapdrawable (bitmap);

Using this constructor, the default density is 160, the constructor has been discarded, and the next one is recommended.

New Bitmapdrawable (Getresources (), bitmap);

The constructor gets the density of the phone through getresources and sets it as the default density.

Getresources (). Getdisplaymetrics (). densitydpi

If we need to scale, we can set the target density by the following method.

Bmpdrawable.settargetdensity ()

On a 320dip-length screen, the bmpdrawable will be reduced to half.

Note: I originally thought that the new bitmapdrawable (Getresources (), bitmap) is the constructor to scale the picture according to the current screen density. If the screen density is 320dip, after the use of the constructor, the picture will automatically enlarge 1 time times (Bitmap object default density is 160), but carefully read the source found that the way is to modify the default density, will be 160 to 320, The density that really needs to be scaled is then set by the Settargetdensity method. I really don't think it's an application scenario.

Android: The difference between different drawable folders

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.