DisplayUtil.java
7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.xdy.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author 蒋洪波
* @version 1.0
* @file DisplayUtil.java
* @brief 获得当前手机一些屏幕信息
* @date 2017/9/22
* Copyright (c) 2017, 学点云
* All rights reserved.
*/
public class DisplayUtil {
/**
* 获得不同手机状态栏高度
* @param ctx
* @return
*/
public static int getStatusBarHeight(Context ctx) {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = ctx.getResources().getDimensionPixelOffset(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
/**
* 魅族手机判断是否有SmartBar 并隐藏SmartBar
* @return
*/
@SuppressWarnings("unused")
public static boolean judgeBar() {
boolean isHasBar = false;
try {
try {
Method method = Class.forName("android.os.Build").getMethod(
"hasSmartBar");
isHasBar = ((Boolean) method.invoke(null)).booleanValue();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return isHasBar;
}
/**
* 全屏显示。。
*/
@SuppressLint("NewApi")
public static void getFullScrean(Activity ctx) {
Window mWindow = ctx.getWindow();
WindowManager.LayoutParams params = mWindow.getAttributes();
params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
mWindow.setAttributes(params);
}
/**
* @param ctx
* @brief 获得屏幕像素密度
*/
public static float getScreenDensity(Context ctx) {
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.density;
}
/**
* @param context
* @param dipValue
* @return
* @brief DP转像素
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* @param context
* @param
* @return
* @brief 像素转DP
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 是否有导航栏
* @param context
* @return
*/
private static boolean checkDeviceHasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();
int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = rs.getBoolean(id);
}
try {
Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
Method m = systemPropertiesClass.getMethod("get", String.class);
String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
if ("1".equals(navBarOverride)) {
hasNavigationBar = false;
} else if ("0".equals(navBarOverride)) {
hasNavigationBar = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return hasNavigationBar;
}
/**
* @param context
* @return
* @brief 获取导航栏(虚拟按键高度)
*/
public static int getNavigationBarHeight(Context context) {
int navigationBarHeight = 0;
Resources rs = context.getResources();
int id = rs.getIdentifier("navigation_bar_height", "dimen", "android");
if (id > 0 && checkDeviceHasNavigationBar(context)) {
navigationBarHeight = rs.getDimensionPixelSize(id);
}
return navigationBarHeight;
}
private static DisplayMetrics dm = null;
public static int getScreenWidth(Context context) {
return getScreenWidthInPx(context);
}
public static int getScreenHeight(Context context) {
return getScreenHeightInPx(context);
}
static public DisplayMetrics getDisplayMetrics(Context context) {
if (dm == null) {
dm = context.getResources().getDisplayMetrics();
}
return dm;
}
static public int px2dp(Context context, int px) {
if (context == null) {
return px;
}
getDisplayMetrics(context);
final float density = dm.density;
return (int) (px / density + 0.5f);
}
static public int dp2px(Context context, float dp) {
if (context == null) {
return (int) dp;
}
getDisplayMetrics(context);
final float density = dm.density;
return (int) (dp * density + 0.5f);
}
static public int px2sp(Context context, float px) {
if (context == null) {
return (int) px;
}
getDisplayMetrics(context);
return (int) (px / dm.scaledDensity + 0.5f);
}
static public int sp2px(Context context, float sp) {
if (context == null) {
return (int) sp;
}
getDisplayMetrics(context);
return (int) (sp * dm.scaledDensity + 0.5f);
}
static public int getScreenWidthInPx(Context context) {
getDisplayMetrics(context);
return dm.widthPixels;
}
static public int getScreenHeightInPx(Context context) {
getDisplayMetrics(context);
return dm.heightPixels;
}
static public int getScreenWidthInDp(Context context) {
getDisplayMetrics(context);
return (int) ((float) dm.widthPixels * (160 / dm.xdpi));
}
static public int getScreenHeightInDp(Context context) {
getDisplayMetrics(context);
int screenHeight = dm.heightPixels;
return (int) ((float) screenHeight / dm.density);
}
static public float getDensity(Context context) {
getDisplayMetrics(context);
return dm.density;
}
public static int getScreenTitleBarHeight(Context context) {
Rect rect = new Rect();
((Activity) context).getWindow().getDecorView()
.getWindowVisibleDisplayFrame(rect);
return rect.top;
}
}