提供了dp互转px,sp互转px,获取屏幕宽高,获取状态栏高度、toolbar高度、底部虚拟菜单高度等等。

dp(dip): device independent pixels(设备独立像素).不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。

使用dp和sp,系统会根据屏幕密度的变化自动进行转换。
如果设置表示长度、高度等属性时可以使用dp。但如果设置字体,需要使用sp。dp是与密度无关,sp除了与密度无关外,还与scale无关。

px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。

sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

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
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
public class DensityUtils {
private static final float DOT_FIVE = 0.5f;
/**
* dip to px
*
* @param context
* @param dip
* @return
*/
public static int dip2px(Context context, float dip) {
float density = getDensity(context);
return (int) (dip * density + DensityUtils.DOT_FIVE);
}
/**
* px to dip
*
* @param context
* @param px
* @return
*/
public static int px2dip(Context context, float px) {
float density = getDensity(context);
return (int) (px / density + DOT_FIVE);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @param pxValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param spValue
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
private static DisplayMetrics sDisplayMetrics;
/**
* get screen width
*
* @param context
* @return
*/
public static int getDisplayWidth(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.widthPixels;
}
/**
* get screen height
*
* @param context
* @return
*/
public static int getDisplayHeight(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.heightPixels;
}
/**
* get screen density
*
* @param context
* @return
*/
public static float getDensity(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.density;
}
/**
* get screen density dpi
*
* @param context
* @return
*/
public static int getDensityDpi(Context context) {
initDisplayMetrics(context);
return sDisplayMetrics.densityDpi;
}
/**
* init display metrics
*
* @param context
*/
private static synchronized void initDisplayMetrics(Context context) {
sDisplayMetrics = context.getResources().getDisplayMetrics();
}
/**
* is landscape
*
* @param context
* @return
*/
public static boolean isLandscape(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
/**
* is portrait
*
* @param context
* @return
*/
public static boolean isPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
public static int getActvieHeight(Activity activity){
Dimension dimen = new Dimension();
// 用户绘制区域
Rect outRect = new Rect();
activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
dimen.mWidth = outRect.width() ;
dimen.mHeight = outRect.height();
// end
return dimen.mHeight;
}
/**
* 获得状态栏的高度
*
* @param activity
* @return
*/
public static int getStatusHeight(Activity activity)
{
// Rect frame = new Rect();
// activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
// int statusBarHeight = frame.top;
// Log.d("111","statusBarHeight"+statusBarHeight);
int statusHeight = -1;
try
{
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height")
.get(object).toString());
statusHeight = activity.getResources().getDimensionPixelSize(height);
} catch (Exception e)
{
e.printStackTrace();
}
return statusHeight;
}
public int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
int height = resources.getDimensionPixelSize(resourceId);
Log.v("dbw", "Navi height:" + height);
return height;
}
public static int getActionBarHeight(Context context) {
TypedValue tv = new TypedValue();
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
return actionBarHeight;
}else{
return 0;
}
}
/**
* get toolbar height
* @param context
* @return
*/
public static int getToolbarHeight(Context context) {
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
return toolbarHeight;
}
private int getTitleHeight(Activity activity) {
int contentTop = activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
//statusBarHeight是上面状态栏的高度
int titleBarHeight = contentTop - getStatusHeight(activity);
return titleBarHeight;
}
private int getActionBarSizeHeight(Activity activity) {
return activity.getActionBar().getHeight();
}
public static class Dimension {
public int mWidth ;
public int mHeight ;
public Dimension(){}
}
/**
* 获取当前屏幕截图,包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getDisplayWidth(activity);
int height = getDisplayHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*
* @param activity
* @return
*/
public static Bitmap snapShotWithoutStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = getDisplayWidth(activity);
int height = getDisplayHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return bp;
}
}

本文地址: http://itdais.com/2016/07/18/屏幕管理工具类/