When creating a new Android project, a few drawable folders will be generated automatically in the Res directory, drawable-ldpi,drawable-mdpi,drawable-hdpi, it's not clear to me all the time, the picture should be placed under which folder, What are the different effects? Used to be simply to create a new drawable folder without a suffix, pictures are thrown in, and now decided to thoroughly understand the matter.
1. Basic Knowledge
Density (density): Simply a scale factor used to convert the dip (device independent pixels) to the actual pixel px. The specific formula is:
px = dip*density+0.5f;
Densitydpi:the screen density expressed asdots-per-inch. Simply say densitydpi = density*160
Drawable folder In addition to these density class suffix, there are, for example,-en for the English environment,-port for vertical screen, etc., here do not discuss, you can refer to http://developer.android.com/guide/topics/ Resources/providing-resources.html
Also attached is an official table of screen size and density:
2. Why to Scale
To adapt to so many messy devices, Android officials recommend that you create different images for different density devices:
36x36 (0.75x) for low-density
48x48 (1.0xbaseline) for medium-density
72x72 (1.5x) for high-density
96x96 (2.0x) for extra-high-density
180x180 (3.0x) for extra-extra-high-density
192x192 (4.0x) for Extra-extra-extra-high-density (launcher icon only; see note above)
The problem is, if you do not listen to the proposal, the whole picture of a density? So when you encounter a different density of mobile phones, the system will be Good (no) heart (sentiment) of your map to zoom, according to the document, this is for your application more beautiful.
Scale formula: Scaled size = picture actual size x (phone density/image density)
Where the image density is determined by the suffix of the drawable folder where the picture resides
For example, a picture of 100x100 in the Mdpi folder, on HDPI's phone, scaled size = 100 * (1.5/1) = 150
It became a picture of 150*150.
3, Android:anydensity
(Some blogs on the internet are wrong to explain this attribute, so here's a special mention)
You can set such a property in the Androidmanifest.xml file: <supports-screens android:anydensity= "true"/>
Default is True if not set.
As the document says (http://developer.android.com/guide/practices/screens_support.html), if true, the scaling mechanism is pre-scaled (pre-scaling), If False, the scaling mechanism is Autoscale (auto-scaling), the difference is that pre-scaling is scaled on read, autoscale when it is drawn, and it is faster to pre-scale from the speed. Another important difference is that if <supports-screensandroid:anydensity= "false"/>, the app will spoof the app when requesting the screen parameters, Tell it you're running on a density 1 phone, regardless of the actual density of the phone, such as the actual phone is hdpi, size 480*800, the system will tell the application screen size is 320 (400/1.5) *533 (800/1.5), Then when the app draws the picture to (10,10) to (100,100) the area, the system will convert it to (15,15) to (150,150), then if you go directly to manipulate the scaled graph, there will be some unexpected problems. In short, it is recommended not to set this property to False.
According to my personal understanding, this false is to tell the system that the application does not support multi-resolution, so the system thinks you only support the default resolution (MDPI), the system will give you a virtual MDPI device, let you display on the above, the system from the top of stretching or shrinking to the actual device. This is both slow and ineffective, so it is not recommended.
4, each directory read priority
Suppose the following drawable directory is in the project:
Drawable
drawable-nodpi
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi.
(If you do not want the system to scale the picture, you can put the picture in the drawable-nodpi directory, the picture system read from this directory does not do any scaling.) )
(The images under the drawable directory without suffixes are treated as drawable-mdpi, as shown below.)
If there may be a picture of the same name in these directories, which one should the system read?
Without a doubt, if the cell phone density of the corresponding density directory has the picture, that is it, if not?
Follow the source to see how the system chooses the image (based on android4.4.2):
Imageview.java:
Setimageresource ()
ResolveUri ()
Resources.java:
Getdrawable ()
GetValue ()
Assetmanager.java:
Getresourcevalue ()
Native Loadresourcevalue ()
Frameworks/base/core/jni/android_util_assetmanager.cpp:
Android_content_assetmanager_loadresourcevalue ()
Frameworks/base/libs/androidfw/assetmanager.cpp:
Assetmanager::getresources ()
Assetmanager::getrestable ()
Frameworks/base/libs/androidfw/resourcetypes.cpp:
Restable::getresource ()
Restable::getentry ()
ssize_t restable::getentry (const package* Package, int typeindex, int entryindex, const restable_config* Config, CO NST restable_type** outtype, const restable_entry** outentry, const type** outtypeclass) const{******** omitted ******* Const size_t NT = Alltypes->configs.size (); for (size_t i=0; i<nt; i++) {Const restable_type* const THISTYPE = alltypes->configs[i]; if (Thistype = = NULL) continue; Restable_config Thisconfig; Thisconfig.copyfromdtoh (Thistype->config); Omit ******* if (type! = NULL) {//Check if this one is less specific than the last found. If So,//We'll skip it. We checkstarting with things we are most care//about to those we least care about. if (!thisconfig.isbetterthan (bestconfig, config)) {//Is here Table_getentry (Alogi ("Thisconfig is worse than L Ast!\n ")); Continue }} type = thisType; offset = Thisoffset; Bestconfig = Thisconfig; Table_getentry (Alogi ("Best entry so far-using it!\n")); if (!config) break; } ******** omit ******* return offset + dtohs (entry->size);}
Restable_config::isbetterthan ()
BOOL Restable_config::isbetterthan (const restable_config& O, const restable_config* requested) const {if (R equested) {************** if (Screentype | | o.screentype) {if (density! = o.density) { Density is tough. Any density is potentially useful//because the system would scale it. Scaling down//are generally better than scaling up. Default density counts as 160DPI (the system default)//Todo-remove Constants int h = (density?density:160); int L = (o.density?o.density:160); bool Bimbigger = true; if (L > h) {int t = h; h = l; L = t; Bimbigger = false; } int reqvalue = (requested->density?requested->density:160); if (reqvalue >= h) {//RequestEd value higher than both L and H, give H return bimbigger; } if (l >= reqvalue) {//requested value lower than both L and H, give L return!bimbigger; }//Saying that scaling down are 2x better than up if (((2 * l)-reqvalue) * H > Reqvalu E * reqvalue) {return!bimbigger; } else {return bimbigger; }} ***********}} return Ismorespecificthan (O);}
Key parts have been marked with red characters, in multiple drawable under the same name picture, a resource ID corresponds to more than one picture, in the getentry there is a loop, with the Isbetterthan () function in the loop to the most suitable picture selected.
As you can see, the default is 160 if the picture does not indicate density,density, which is why the picture under the Drawable folder is default to MDPI.
In the Isbetterthan function, the density is the density of the current resource, o.density is the density of the most suitable resource in the previous cycle, and Reqvalue is the request density.
Three If,
The first if: if density and o.density are less than reqvalue, then the larger one is more appropriate
The second if: if density and o.density are larger than Reqvalue, then the smaller one is more appropriate.
Third if: If the reqvalue size is between density and o.density, first judge
if ((((2 * l)-reqvalue) * h > Reqvalue * reqvalue)
The effect of this judgment is that the request density and the smaller density difference is very small, and the larger one density difference is very big. Then it is considered that the smaller density is more appropriate.
Test environment: Simulator +android4.4.2, where XH and xxh are +android4.4.2 measured with a real machine, ldpi is used Android4.4.2 except Android2.3.1, HDPI except Android4.4.2 also uses Android2.1, the result is not different.
Test Catalog: Drawable-ldpi,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-nodpi,drawable
Test result ():
How drawable-nodpi sometimes in front of the back? With two sources, you'll see.
Frameworks/base/include/androidfw/resourcetypes.h:
enum { Density_default = aconfiguration_density_default, density_low =aconfiguration_density_low, Density_medium =aconfiguration_density_medium, density_tv = Aconfiguration_density_tv, DENSITY_HIGH = Aconfiguration_density_high, Density_xhigh = Aconfiguration_density_xhigh, density_xxhigh =ACONFIGURATION _density_xxhigh, density_xxxhigh =aconfiguration_density_xxxhigh, density_none =aconfiguration_density _none };
Frameworks/native/include/android/configuration.h:
Aconfiguration_density_default = 0, Aconfiguration_density_low = +, aconfiguration_density_medium = 160, Aconfiguration_density_tv = 213, Aconfiguration_density_high = Aconfiguration_density_xhigh = +, Aconfiguration_density_xxhigh = 480, Aconfiguration_density_xxxhigh = 640, Aconfiguration_density_none = 0xFFFF,
Visible in the drawable-nodpi directory, the image density value is 0xffff, that is, 65535, with a calculation, the results as measured.
No picture no truth, so put a hdpi environment test map:
To know which graph to read, you can:
Typedvalue typedvalue = new Typedvalue (); Getresources (). GetValue (r.drawable.test,typedvalue,true);// Then the typedvalue.string value is the picture path actually read
Android Resource picture reading mechanism