- 1、px、dp、sp相互转换方法
- 2、屏幕宽高px dp获取方法
- 3、屏幕密度 dpi获取方法
/**
* @author: huchunhua
* @time: 2019/5/9
* @package:
* @project:
* @mail: huachunhu@qq.com
* @describe: px、dp、sp转换And屏幕相关信息获取工具类
*/
public class DisplayUtil {
private static final String TAG = "DisplayUtil";
/**
* 获取屏幕密度(0.75 / 1.0 / 1.5)
* @param context
* @return
*/
public float getScreenDensity(Context context) {
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5)
Log.d(TAG, "getScreenDensity: density = " + density);
return density;
}
/**
* 获取屏幕密度(0.75 / 1.0 / 1.5)
* @param context
* @return
*/
public int getScreenDensityDpi(Context context) {
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int densityDpi = dm.densityDpi; // 屏幕密度dpi(120 / 160 / 240)
Log.d(TAG, "getScreenDensityDpi: densityDpi = " + densityDpi);
return densityDpi;
}
/**
* 获取屏幕宽度 单位dp
* @param context
* @return
*/
public int getDpScreenWidth(Context context){
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度(像素)
float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5)
// 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
int screenWidth = (int) (width / density); // 屏幕宽度(dp)
Log.d(TAG, "getPxScreenWidth: screenWidth = " + screenWidth);
return screenWidth;
}
/**
* 获取屏幕宽度 单位px
* @param context
* @return
*/
public int getPxScreenWidth(Context context){
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度(像素)
Log.d(TAG, "getPxScreenWidth: width = " + width);
return width;
}
/**
* 获取屏幕高度 单位px
* @param context
* @return
*/
public int getPxScreenHeight(Context context){
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels; // 屏幕高度(像素)
Log.d(TAG, "getPxScreenHeight: height = " + height);
return height;
}
/**
* 获取屏幕高度 单位dp
* @param context
* @return
*/
public int getDpScreenHeight(Context context){
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int height = dm.heightPixels; // 屏幕高度(像素)
float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5)
// 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
int screenHeight = (int) (height / density);// 屏幕高度(dp)
Log.d(TAG, "getDpScreenHeight: screenHeight = " + screenHeight);
return screenHeight;
}
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param pxValue
* (DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param dipValue
* (DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
public void getScreenProperty(Context context) {
WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度(像素)
int height = dm.heightPixels; // 屏幕高度(像素)
float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = dm.densityDpi; // 屏幕密度dpi(120 / 160 / 240)
// 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
int screenWidth = (int) (width / density); // 屏幕宽度(dp)
int screenHeight = (int) (height / density);// 屏幕高度(dp)
Log.d(TAG, "屏幕宽度(像素):" + width);
Log.d(TAG, "屏幕高度(像素):" + height);
Log.d(TAG, "屏幕密度(0.75 / 1.0 / 1.5):" + density);
Log.d(TAG, "屏幕密度dpi(120 / 160 / 240):" + densityDpi);
Log.d(TAG, "屏幕宽度(dp):" + screenWidth);
Log.d(TAG, "屏幕高度(dp):" + screenHeight);
}
}
网友评论