Skip to content

Commit

Permalink
兼容dp2px
Browse files Browse the repository at this point in the history
  • Loading branch information
pichsy committed Feb 4, 2021
1 parent 06372e2 commit 20cbd09
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


implementation 'com.github.pichsy:xwidget:1.7'
implementation 'com.github.pichsy:xwidget:1.8'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,6 @@ public static int getScreenHeight(Context context) {
public static int[] getRealScreenSize(Context context) {
// 切换屏幕导致宽高变化时不能用 cache,先去掉 cache
return doGetRealScreenSize(context);
// if (XDeviceHelper.isEssentialPhone() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// // Essential Phone 8.0版本后,Display size 会根据挖孔屏的设置而得到不同的结果,不能信任 cache
// return doGetRealScreenSize(context);
// }
// int orientation = context.getResources().getConfiguration().orientation;
// int[] result;
// if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// result = sLandscapeRealSizeCache;
// if (result == null) {
// result = doGetRealScreenSize(context);
// if(result[0] > result[1]){
// // the result may be wrong sometimes, do not cache !!!!
// sLandscapeRealSizeCache = result;
// }
// }
// return result;
// } else {
// result = sPortraitRealSizeCache;
// if (result == null) {
// result = doGetRealScreenSize(context);
// if(result[0] < result[1]){
// // the result may be wrong sometimes, do not cache !!!!
// sPortraitRealSizeCache = result;
// }
// }
// return result;
// }
}

private static int[] doGetRealScreenSize(Context context) {
Expand All @@ -182,15 +155,12 @@ private static int[] doGetRealScreenSize(Context context) {
// used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)
Point realSize = new Point();
d.getRealSize(realSize);


Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}
}

size[0] = widthPixels;
size[1] = heightPixels;
return size;
Expand Down Expand Up @@ -228,7 +198,6 @@ public static int getUsefulScreenWidth(Context context, boolean hasNotch) {
if (XDeviceHelper.isHuawei() && !XDisplayHelper.huaweiIsNotchSetToShowInSetting(context)) {
result -= XNotchHelper.getNotchSizeInHuawei(context)[1];
}

// TODO vivo 设置-系统导航-导航手势样式-显示手势操作区域 打开的情况下,应该减去手势操作区域的高度,但无API
// TODO vivo 设置-显示与亮度-第三方应用显示比例 选为安全区域显示时,整个 window 会移动,应该减去移动区域,但无API
// TODO oppo 设置-显示与亮度-应用全屏显示-凹形区域显示控制 关闭是,整个 window 会移动,应该减去移动区域,但无API
Expand Down Expand Up @@ -292,7 +261,15 @@ public static boolean isNavMenuExist(Context context) {
public static int dp2px(Context context, float dp) {
return (int) (getDensity(context) * dp + 0.5);
}

/**
* 单位转换: dp -> px
*
* @param dp
* @return
*/
public static int dp2px(Context context, int dp) {
return (int) (getDensity(context) * dp + 0.5);
}
/**
* 单位转换: sp -> px
*
Expand All @@ -303,6 +280,16 @@ public static int sp2px(Context context, float sp) {
return (int) (getFontDensity(context) * sp + 0.5);
}

/**
* 单位转换: sp -> px
*
* @param sp
* @return
*/
public static int sp2px(Context context, int sp) {
return (int) (getFontDensity(context) * sp + 0.5);
}

/**
* 单位转换:px -> dp
*
Expand Down

0 comments on commit 20cbd09

Please sign in to comment.