package com.example.administrator.filemanager.utils;
import android.content.Context;
import android.view.WindowManager;
/**
*单位换算,加获取屏幕的宽度和高度(像素)
*/
public class DeviceUtil {
/*
* 单位换算
* */
public static int dp2px(Context context,int dp){
float num=context.getResources().getDisplayMetrics().density;
return (int)(dp*num+0.5f);
}
public static int px2dp(Context context,int px){
float num2=context.getResources().getDisplayMetrics().density;
return(int)(px/num2+0.5f);
}
/*
* 获取屏幕宽度:重要记住
* */
public static int getScreenWidthPx(Context context){
WindowManager windowManager= (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
return windowManager.getDefaultDisplay().getWidth();
}
/*
* 获取屏幕高度
* */
public static int getScreenHeightPx(Context context){
WindowManager windowManager= (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
return windowManager.getDefaultDisplay().getHeight();
}
}