大神Github:https://github.com/JessYanCoding/AndroidAutoSize.git
px = density*dp; //像素
density = dpi/160;//像素密度
dpi = √(x²+y²)/屏幕尺寸;//dot per inch 每英寸像素点数
width_dp = width_px/density;//宽度dp
height_dp = height_px/density;//高度dp
由于px = density*dp,dp为设计图固定值,想要px宽度/高度恒定,那么只有改变density变量。
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int densityDpi = displayMetrics.densityDpi;
float scaledDensity = displayMetrics.scaledDensity;
int widthPixels = displayMetrics.widthPixels;
int heightPixels = displayMetrics.heightPixels;
float xdpi = displayMetrics.xdpi;
float ydpi = displayMetrics.ydpi;
double size = Math.sqrt((widthPixels / xdpi) * (widthPixels / xdpi) + (heightPixels / ydpi) * (heightPixels / ydpi));
double dpi = Math.sqrt(widthPixels * widthPixels + heightPixels * heightPixels) / size;
Log.i("Simon","densityDpi = " + densityDpi
+" scaledDensity = " + scaledDensity
+" widthPixels = " + widthPixels
+" heightPixels = " + heightPixels
+" screenSize = " + screenSize
+" screenDpi = " + screenDpi
);