Skip to content

Commit

Permalink
调整px2dp
Browse files Browse the repository at this point in the history
  • Loading branch information
pichsy committed Jan 7, 2021
1 parent f825dd6 commit 46208e4
Showing 1 changed file with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@
* @date 2016-03-17
*/
public class XDisplayHelper {
private static final String TAG = "XDisplayHelper";

/**
* 屏幕密度,系统源码注释不推荐使用
*/
public static final float DENSITY = Resources.getSystem()
.getDisplayMetrics().density;
private static final String TAG = "XDisplayHelper";
public static final float DENSITY = Resources.getSystem().getDisplayMetrics().density;

/**
* 是否有摄像头
Expand Down Expand Up @@ -117,7 +116,7 @@ public static int getScreenWidth(Context context) {
*/
public static int getScreenHeight(Context context) {
int screenHeight = getDisplayMetrics(context).heightPixels;
if(XDeviceHelper.isXiaomi() && xiaomiNavigationGestureEnabled(context)){
if (XDeviceHelper.isXiaomi() && xiaomiNavigationGestureEnabled(context)) {
screenHeight += getResourceNavHeight(context);
}
return screenHeight;
Expand Down Expand Up @@ -265,9 +264,9 @@ private static int getUsefulScreenHeight(Context context, boolean hasNotch) {
return result;
}
// if (isPortrait) {
// TODO vivo 设置-系统导航-导航手势样式-显示手势操作区域 打开的情况下,应该减去手势操作区域的高度,但无API
// TODO vivo 设置-显示与亮度-第三方应用显示比例 选为安全区域显示时,整个 window 会移动,应该减去移动区域,但无API
// TODO oppo 设置-显示与亮度-应用全屏显示-凹形区域显示控制 关闭是,整个 window 会移动,应该减去移动区域,但无API
// TODO vivo 设置-系统导航-导航手势样式-显示手势操作区域 打开的情况下,应该减去手势操作区域的高度,但无API
// TODO vivo 设置-显示与亮度-第三方应用显示比例 选为安全区域显示时,整个 window 会移动,应该减去移动区域,但无API
// TODO oppo 设置-显示与亮度-应用全屏显示-凹形区域显示控制 关闭是,整个 window 会移动,应该减去移动区域,但无API
// }
return result;
}
Expand All @@ -290,7 +289,7 @@ public static boolean isNavMenuExist(Context context) {
* @param dp
* @return
*/
public static int dp2px(Context context, int dp) {
public static int dp2px(Context context, float dp) {
return (int) (getDensity(context) * dp + 0.5);
}

Expand All @@ -300,7 +299,7 @@ public static int dp2px(Context context, int dp) {
* @param sp
* @return
*/
public static int sp2px(Context context, int sp) {
public static int sp2px(Context context, float sp) {
return (int) (getFontDensity(context) * sp + 0.5);
}

Expand All @@ -310,8 +309,8 @@ public static int sp2px(Context context, int sp) {
* @param px
* @return
*/
public static int px2dp(Context context, int px) {
return (int) (px / getDensity(context) + 0.5);
public static float px2dp(Context context, int px) {
return (float) px / getDensity(context);
}

/**
Expand All @@ -320,8 +319,8 @@ public static int px2dp(Context context, int px) {
* @param px
* @return
*/
public static int px2sp(Context context, int px) {
return (int) (px / getFontDensity(context) + 0.5);
public static float px2sp(Context context, int px) {
return (float) px / getFontDensity(context);
}

/**
Expand Down Expand Up @@ -362,7 +361,7 @@ public static int getActionBarHeight(Context context) {
* @return
*/
public static int getStatusBarHeight(Context context) {
if(XDeviceHelper.isXiaomi()){
if (XDeviceHelper.isXiaomi()) {
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return context.getResources().getDimensionPixelSize(resourceId);
Expand All @@ -374,7 +373,7 @@ public static int getStatusBarHeight(Context context) {
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
if(x > 0){
if (x > 0) {
return context.getResources().getDimensionPixelSize(x);
}
} catch (Exception e) {
Expand Down Expand Up @@ -402,7 +401,7 @@ public static int getNavMenuHeight(Context context) {
return getRealScreenSize(context)[1] - getScreenHeight(context);
}

private static int getResourceNavHeight(Context context){
private static int getResourceNavHeight(Context context) {
// 小米4没有nav bar, 而 navigation_bar_height 有值
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
Expand Down

0 comments on commit 46208e4

Please sign in to comment.