/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
LogUtil.d("density-----------------"+scale);
int result=0;
if (scale==1){
result=(int) (dpValue * scale + 0.5f);
}
if (scale==1.5){
result=(int) (dpValue * scale/1.5 + 0.5f);
}
if (scale==2){
result= (int) (dpValue * scale/2 + 0.5f);
}
return result;
}
/**
* 判断设备是否是平板设备 true表示平板 false 表示手机
* @param context
* @return
*/
public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
/**
* 获取指定的不同分辨率的dialog高度
* @param context
* @return
*/
public static int getDialogH(Context context) {
int result = 0;
int temp = getScreenHeight(context);
LogUtil.d("getScreenHeight - " + temp);
if (temp <=480){
result = ScreenUtil.dip2px(context, ResourceUtil.getXmlDef(context, R.dimen.dialog_pad_height_one));
}
if (temp <=540){
result = ScreenUtil.dip2px(context, ResourceUtil.getXmlDef(context, R.dimen.dialog_pad_height_two));
}
if(temp <= 800) {
result = ScreenUtil.dip2px(context, ResourceUtil.getXmlDef(context, R.dimen.dialog_pad_height_three));
} else if(temp <=1080) {
result = ScreenUtil.dip2px(context, ResourceUtil.getXmlDef(context, R.dimen.dialog_pad_height_four));
}else{
result = ScreenUtil.dip2px(context, ResourceUtil.getXmlDef(context, R.dimen.dialog_pad_height_five));
}
return result;
}
}
/**
* 获得屏幕宽度
*
* @param context
* @return 单位 px
*/
public static int getScreenHeight(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.heightPixels;
}
/**
* 获得屏幕高度
*
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
return outMetrics.widthPixels;
}
Window dialogWindow = dialog.getWindow();
//WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//dialogWindow.setGravity(Gravity.CENTER);
//lp.width = ScreenUtil.dip2px(this, ResourceUtil.getXmlDef(this, R.dimen.dialog_user_select_width)); // 宽度
//lp.height = ScreenUtil.dip2px(this, ResourceUtil.getXmlDef(this, R.dimen.dialog_user_select_height)); // 高度
//lp.alpha = 0.7f; // 透明度
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
if (AndroidApplication.isTablet) {
// p.height = (int) (ScreenUtil.getScreenHeight(this) * 0.70); // 高度设置为屏幕的0.70
p.height = ScreenUtil.getDialogH(this);
} else {
p.height = (int) (ScreenUtil.getScreenHeight(this) * 0.45); // 高度设置为屏幕的0.45
}
p.width = (int) (ScreenUtil.getScreenWidth(this) * 0.75); // 宽度设置为屏幕的0.75
dialogWindow.setAttributes(p);