Displaymetrics metrice= new Displaymetrics ();
Display Defaultdisplay = Getwindowmanager (). Getdefaultdisplay ();
Defaultdisplay.getmetrics (Metrice);
int densitydpi = metrice.densitydpi;
float density = metrice.density;
int heightpixels = Metrice.heightpixels;
int widthpixels = Metrice.widthpixels;
float xdpi = metrice.xdpi;
float ydpi = metrice.ydpi;
float scaleddensity = metrice.scaleddensity;
Dip: In device-independent pixel values, such as I have a value of 50dip on medium density, the value displayed on a low-density device is 50*0.75=37.5px
PX: Represents the physical pixel, for example, the value shown on the medium density is 50px, the unit is PX, the value displayed on other density devices is also 50px
DENSITYDPI: Number of dots per inch
density:densitydpi/160;
drawable-hdpi high density, usually referred to as the 240 densitydpi in the above code is 240
drawable-ldpi bottom density, usually referred to as 120
DRAWABLE-MDPI medium density, usually referred to as 160
DRAWABLE-XHDPI ultra-high density, usually referred to as 320
Put a picture of 48*48 in drawable-mdpi, set the picture to the background of button buttons, the data obtained under different density, different resolution devices are as follows:
densitydpi=120
density=0.75
heightpixels=320
widthpixels=240
xdpi=120.0
ydpi=120.0
scaleddensity=0.75
intrinsicheight=36
intrinsicwidth=36
densitydpi=160
density=1.0
heightpixels=480
widthpixels=320
xdpi=160.0
ydpi=160.0
scaleddensity=1.0
intrinsicheight=48
intrinsicwidth=48
densitydpi=160
density=1.0
heightpixels=768
widthpixels=1024
xdpi=160.0
ydpi=160.0
scaleddensity=1.0
intrinsicheight=48
intrinsicwidth=48
densitydpi=240
density=1.5
heightpixels=800
widthpixels=480
xdpi=240.0
ydpi=240.0
scaleddensity=1.5
intrinsicheight=72
intrinsicwidth=72
densitydpi=240
density=1.5
heightpixels=800
widthpixels=480
xdpi=160.42105
ydpi=160.0
scaleddensity=1.5
dimension=75.0
intrinsicheight=72
intrinsicwidth=72
densitydpi=320
density=2.0
heightpixels=640
widthpixels=960
xdpi=320.0
ydpi=320.0
scaleddensity=2.0
INTRINSICHEIGHT=96//Represents the physical pixels that the picture actually displays
intrinsicwidth=96
It can be concluded that there is a great relationship between the image display and density, regardless of the set resolution at the same density, the size of the picture is the same, in the case of the density is not the same, the picture will be scaled: for example, in the medium density film display height of 48px, low density display height of 48*0.75= 36, the picture will be reduced, the height of the display at high density is 48*1.5=72, the picture will enlarge;
This can also be derived from the official website of px = dp * (dpi / 160) 即px =dp*density;(density=densityDpi/160) the calculation formula:
The resolution of Android devices