-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDensityUtils.java
More file actions
executable file
·289 lines (265 loc) · 8.39 KB
/
DensityUtils.java
File metadata and controls
executable file
·289 lines (265 loc) · 8.39 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.youth.xframe.utils;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.youth.xframe.XFrame;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 屏幕、尺寸、View相关工具类
*/
public class XDensityUtils {
private XDensityUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* dp转px
*
* @param dpValue dp值
* @return px值
*/
public static int dp2px(float dpValue) {
final float scale = XFrame.getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px转dp
*
* @param pxValue px值
* @return dp值
*/
public static int px2dp( float pxValue) {
final float scale = XFrame.getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* sp转px
*
* @param spValue sp值
* @return px值
*/
public static int sp2px(float spValue) {
final float fontScale = XFrame.getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* px转sp
*
* @param pxValue px值
* @return sp值
*/
public static int px2sp( float pxValue) {
final float fontScale = XFrame.getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 各种单位转换
* <p>该方法存在于TypedValue</p>
*
* @param unit 单位
* @param value 值
* @param metrics DisplayMetrics
* @return 转换结果
*/
public static float applyDimension(int unit, float value, DisplayMetrics metrics) {
switch (unit) {
case TypedValue.COMPLEX_UNIT_PX:
return value;
case TypedValue.COMPLEX_UNIT_DIP:
return value * metrics.density;
case TypedValue.COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case TypedValue.COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f / 72);
case TypedValue.COMPLEX_UNIT_IN:
return value * metrics.xdpi;
case TypedValue.COMPLEX_UNIT_MM:
return value * metrics.xdpi * (1.0f / 25.4f);
}
return 0;
}
/**
* 获取屏幕的宽度
* @return
*/
public static int getScreenWidth() {
return XFrame.getDisplayMetrics().widthPixels;
}
/**
* 获取屏幕的高度
* @return
*/
public static int getScreenHeight() {
return XFrame.getDisplayMetrics().heightPixels;
}
/**
* 获取屏幕真正的高度
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getScreenRealHeight() {
int h;
WindowManager winMgr = (WindowManager) XFrame.getSystemService(Context.WINDOW_SERVICE);
Display display = winMgr.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealMetrics(dm);
h = dm.heightPixels;
} else {
try {
Method method = Class.forName("android.view.Display").getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
h = dm.heightPixels;
} catch (Exception e) {
display.getMetrics(dm);
h = dm.heightPixels;
}
}
return h;
}
/**
* 获取顶部状态栏高度
* @return
*/
public static int getStatusBarHeight() {
Class<?> c;
Object obj;
Field field;
int statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = XFrame.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
/**
* 获取底部导航栏高度
* @return
*/
public static int getNavigationBarHeight() {
int navigationBarHeight = 0;
Resources resources = XFrame.getResources();
int resourceId = resources.getIdentifier(resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");
if (resourceId > 0 && checkDeviceHasNavigationBar()) {
navigationBarHeight = resources.getDimensionPixelSize(resourceId);
}
return navigationBarHeight;
}
/**
* 检测是否具有底部导航栏。(一加手机上判断不准确,希望有猿友能修复这个 bug)
*
* @return
*/
public static boolean checkDeviceHasNavigationBar() {
boolean hasNavigationBar = false;
Resources resources = XFrame.getResources();
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
if (id > 0) {
hasNavigationBar = resources.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) {
}
return hasNavigationBar;
}
/**
* 在onCreate中获取视图的尺寸
* <p>需回调onGetSizeListener接口,在onGetSize中获取view宽高</p>
* <p>用法示例如下所示</p>
* <pre>
* SizeUtils.forceGetViewSize(view, new SizeUtils.onGetSizeListener() {
* Override
* public void onGetSize(View view) {
* view.getWidth();
* }
* });
* </pre>
*
* @param view 视图
* @param listener 监听器
*/
public static void forceGetViewSize(final View view, final onGetSizeListener listener) {
view.post(new Runnable() {
@Override
public void run() {
if (listener != null) {
listener.onGetSize(view);
}
}
});
}
/**
* 获取到View尺寸的监听
*/
public interface onGetSizeListener {
void onGetSize(View view);
}
public static void setListener(onGetSizeListener listener) {
mListener = listener;
}
private static onGetSizeListener mListener;
/**
* 测量视图尺寸
*
* @param view 视图
* @return arr[0]: 视图宽度, arr[1]: 视图高度
*/
public static int[] measureView(View view) {
ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp == null) {
lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
}
int widthSpec = ViewGroup.getChildMeasureSpec(0, 0, lp.width);
int lpHeight = lp.height;
int heightSpec;
if (lpHeight > 0) {
heightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight, View.MeasureSpec.EXACTLY);
} else {
heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
view.measure(widthSpec, heightSpec);
return new int[]{view.getMeasuredWidth(), view.getMeasuredHeight()};
}
/**
* 获取测量视图宽度
*
* @param view 视图
* @return 视图宽度
*/
public static int getMeasuredWidth(View view) {
return measureView(view)[0];
}
/**
* 获取测量视图高度
*
* @param view 视图
* @return 视图高度
*/
public static int getMeasuredHeight(View view) {
return measureView(view)[1];
}
}