What is DPI?
DPI is the abbreviation for "dot per inch" and the number of pixels per inch.
Four density classifications: ldpi (Low), MDPI (Medium), hdpi (High), and xhdpi (extra high)
General screen: LDPI is 120,mdpi is 160,hdpi is 240,xhdpi is 320.
The DPI calculation formula
dpi= diagonal pixel values/dimensions
Phone screen resolution and screen density are different! Not the 800*480 resolution of the phone picture should be placed in the hdpi folder. 5.0 inch 800*480 belongs to MDPI
It can also be obtained by code:
Displaymetrics metric = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
int width = metric.widthpixels; Screen width (pixels)
int height = metric.heightpixels; Screen height (pixels)
float density = metric.density; Screen Density (0.75/1.0/1.5)
int densitydpi = metric.densitydpi; Screen density dpi (120/160/240)
Depending on the size and density of the screen itself, Android automatically loads the corresponding resources and converts them from logical pixels (dips, which define the interface layout) to physical pixels on the screen.
First look at how the system uses the resources getdrawable (int id) method to find the image
Public drawable getdrawable (int id) throws Notfoundexception {
Typedvalue value;
Synchronized (Maccesslock) {
value = Mtmpvalue;
if (value = = null) {
Value = new Typedvalue ();
} else {
Mtmpvalue = null;
}
GetValue (ID, value, true);
}
drawable res = loaddrawable (value, id);
Synchronized (Maccesslock) {
if (Mtmpvalue = = null) {
Mtmpvalue = value;
}
}
return res;
}
Typedvalue We can understand the type of data stored, primarily used by resouces to hold the value of the resource
The property of the resource that gets the ID through the GetValue (id,value,true) method
public void getValue (int id, Typedvalue outvalue, Boolean resolverefs)
Throws Notfoundexception {
Boolean found = Massets.getresourcevalue (ID, 0, outvalue, resolverefs);
if (found) {
Return
}
throw new Notfoundexception ("Resource ID #0x"
+ integer.tohexstring (ID));
}
Finally through the loaddrawable (value, id) to get drawable, the method to the bottom of the C code, the general meaning is through the Typedvalue methods and properties can get the property values we want, and then load the picture
OK, let's do an experiment below.
Test one: The phone is 1280*720 4.3 inch belonging to the xdpi picture resolution of 960*640 (according to the normal picture does not scale the picture placed in the phone should not be filled with the entire cell phone) I put the picture in not
Folder load time (ms) picture Display description
Drawable 311 Full screen picture with stretching
DRAWABLE-NODPI 130 not full screen picture showing normal
DRAWABLE-LDPI 442 full screen picture with stretching
DRAWABLE-MDPI 383 full screen picture with stretching
drawable-hdpi 226 full screen picture with stretching
DRAWABLE-XHDPI 109 not full screen picture showing normal
Test two: The phone is 800*480 4.3 inch belonging to the hdpi picture resolution of 960*640 (according to the normal picture does not scale the picture placed in the phone should be filled with the entire cell phone) I put the picture in a different drawable folder
Folder load time (ms) picture Display description
drawable 290 Full Screen picture stretching
DRAWABLE-NODPI 127 Full Screen picture shows normal
DRAWABLE-LDPI 369 full Screen picture stretching
DRAWABLE-MDPI 346 full Screen picture stretching
DRAWABLE-HDPI 124 Full screen picture shows normal
DRAWABLE-XHDPI 241 not full screen picture zoom
Get the conclusion:
Images in drawable-nodpi are not stretched
When the system gets the picture, it will go to the DPI folder of the device to find the resource file, and the image should not be scaled directly back.
If you do not find it in the corresponding DPI folder, go back to the other folder to find it, and then zoom accordingly.
Images found at high dpi are scaled, and at low DPI will stretch
There is the source code can be seen loaddrawable process is really C-level do. Loading the system resource ID is more convenient than loading the picture directly from the Java layer.
Android Development resolution problem Solution