First introduced to the respective definitions:
ppi : Pixel per inch, the number of pixels per inch, the higher the value, the more delicate screen
dpi : DOT per inch, dots per inch, the more value is high, the more delicate images
DP : DIP, pixel density-Independent, Andrews development unit of length is used, the screen indicates 1DP pixel density of 160ppi length when 1px
Equation:
2. take a screen density
3.
Get the width of the screen real number dp
4. Get the number of the height of the screen is operable region dp
5. Get the number of screens true width dp
6. Gets the height of the number of screen real dp
7. determined whether a flat plate
The most basic units of a picture or photograph on the pixel, pixels, electronic screen: px
pt : Point, point, the printing industry common units, equal to 1/72 of an inchppi : Pixel per inch, the number of pixels per inch, the higher the value, the more delicate screen
dpi : DOT per inch, dots per inch, the more value is high, the more delicate images
DP : DIP, pixel density-Independent, Andrews development unit of length is used, the screen indicates 1DP pixel density of 160ppi length when 1px
SP : Scale-work of the Independent Pixel, Android developers font size of the unit used.
Wherein px, pt, dp units of length, ppi and dpi is a unit density
density | ldpi | mdpi | hdpi | xhdpi | xxhdpi |
---|---|---|---|---|---|
Density | 120 | 160 | 240 | 320 | 480 |
Resolution | 240*320 | 320*480 | 480*800 | 720*1280 | 1080*1920 |
Icon Size | 36*36 | 48*48 | 72*72 | 96*96 | 144*144 |
Equation:
= The PPI
√ (Y * X * X + Y) / Z formula length X is the number of pixels, Y is the number of pixels width, Z is the screen size.
For example: Iphone 6s 4.7 inch a density of 2000 × 1125 pixels
√
(2000 + 2000 * 1125. 1125. *) / 4.7 = 488ppi
Common methods:
1.
Gets the screen height
public int getRealHeight(Activity activity) {
int heightPixels = 0;
Display display = activity.getWindowManager().getDefaultDisplay();
final int VERSION = Build.VERSION.SDK_INT;
if(VERSION < 13) {
display.getHeight();
}else if (VERSION == 13) {
try {
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (Exception e) {
}
} else {
Point realSize = new Point();
try {
Display.class.getMethod("getRealSize", Point.class).invoke(display, realSize);
heightPixels = realSize.y;
} catch (Exception e) {
}
}
return heightPixels;
}
2. take a screen density
public static float getScreenDensity(Activity activity) {
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.density;
}
public static float getScreenWidthDp(Activity activity) {
return getScreenWidth(activity) / getScreenDensity(activity);
}
4. Get the number of the height of the screen is operable region dp
public static float getScreenHeightDp(Activity activity) {
return getScreenHeight(activity) / getScreenDensity(activity);
}
5. Get the number of screens true width dp
public static float getRealWidthDp(Activity activity) {
return getRealWidth(activity) / getScreenDensity(activity);
}
6. Gets the height of the number of screen real dp
public static float getRealHeightDp(Activity activity) {
return getRealHeight(activity) / getScreenDensity(activity);
}
7. determined whether a flat plate
public static boolean isTablet(Activity activity) {
return isMoreThan6Inch(activity) && isScreenSizeLarge(activity);
}
/**
* 判断是否大于6英寸
* @param activity
* @return
*/
public static boolean isMoreThan6Inch(Activity activity) {
return getScreenInch(activity) >= 6.0;
}
/**
* 判断设备是否为大尺寸屏幕
*
* @param context
* @return
*/
public static boolean isScreenSizeLarge(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}