-
Notifications
You must be signed in to change notification settings - Fork 802
/
AppScreenMgr.java
367 lines (337 loc) · 11.2 KB
/
AppScreenMgr.java
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.jingewenku.abrahamcaijin.commonutil;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Rect;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 主要功能:有关Android屏幕的工具类
* @Prject: CommonUtilLibrary
* @Package: com.jingewenku.abrahamcaijin.commonutil
* @author: AbrahamCaiJin
* @date: 2017年05月04日 14:13
* @Copyright: 个人版权所有
* @Company:
* @version: 1.0.0
*/
public class AppScreenMgr {
/**
* Get the width of the screen.
*获得屏幕宽度
* @param context
* The context to use. Usually your
* {@link android.app.Application} or
* {@link Activity} object.
* @return Return the width of the screen.
*/
public static int getScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.widthPixels;
}
/**
* Get the height of the screen.
*获得屏幕高度
* @param context
* The context to use. Usually your
* {@link android.app.Application} or
* {@link Activity} object.
* @return Return the height of the screen.
*/
public static int getScreenHeight(Context context) {
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(metrics);
return metrics.heightPixels;
}
/**
*
* Whether the Status bar is hidden or not,the method always helps you get
* the height of Status bar.
*获得状态栏的高度
* @param context
* The context to use. Usually your
* {@link android.app.Application} or
* {@link Activity} object.
* @return Return the height of Status bar.
*/
public static int getStatusHeight(Context context) {
int statusHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int id = (Integer) (clazz.getField("status_bar_height").get(object));
statusHeight = context.getResources().getDimensionPixelSize(id);
} catch (Exception e) {
e.printStackTrace();
}
return statusHeight;
}
/**
* 功能描述:获取整块屏幕的高度
*
* @param context
* @return int
*/
public static int getRealScreenHeight(Context context) {
int dpi = 0;
Display display = ((Activity) context).getWindowManager()
.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
@SuppressWarnings("rawtypes")
Class c;
try {
c = Class.forName("android.view.Display");
@SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
dpi = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return dpi;
}
/**
* 功能描述:获取虚拟按键区域的高度
*
* @param context
* @return int 如果有虚拟按键则返回其高度否则返回0;
*/
public static int getNavigationAreaHeight(Context context) {
int realScreenHeight = AppScreenMgr.getRealScreenHeight(context);
int screenHeight = AppScreenMgr.getScreenHeight(context);
return realScreenHeight - screenHeight;
}
/**
* 获取导航栏高度
* @param c
* @return
*/
public static int getNavigationBarrH(Context c) {
Resources resources = c.getResources();
int identifier = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelOffset(identifier);
}
private AppScreenMgr()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 获取当前屏幕截图,包含状态栏
*/
public static Bitmap snapShotWithStatusBar(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
int width = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
view.destroyDrawingCache();
return bp;
}
/**
* 获取当前屏幕截图,不包含状态栏
*/
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 = getScreenWidth(activity);
int height = getScreenHeight(activity);
Bitmap bp = null;
bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bp;
}
/**
* 获得标题栏高度
*
* @param context 上下文,为Activity对象
* @return 标题栏高度
*/
public static int getTitleBarHeight(Activity context) {
int contentTop = context.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
return contentTop - getStatusBarHeight(context);
}
/**
* 获取通知栏高度
*
* @param context 上下文
* @return 通知栏高度
*/
public static int getStatusBarHeight(Context context) {
int statusBarHeight = 0;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object obj = clazz.newInstance();
Field field = clazz.getField("status_bar_height");
int temp = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(temp);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}
/**
* 获取指定Activity的截屏,保存到png文件
*
* @param activity activity
* @return 截屏Bitmap
*/
private static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i("TAG", "" + statusBarHeight);
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
/**
* 保存bitmap
*
* @param b bitmap
* @param strFileName 文件名
* @return 是否保存成功
*/
private static boolean savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
return true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 截取webView快照(webView加载的整个内容的大小)
*
* @param webView webview
* @return 截屏bitmap
*/
private static Bitmap captureWebView(WebView webView) {
Picture snapShot = webView.capturePicture();
Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
snapShot.draw(canvas);
return bmp;
}
/**
* 根据毫秒获得格式化日期
*
* @param time 毫秒数
* @param format 格式化字符串
* @return 格式化后的字符串
*/
private static String getDate(long time, String format) {
Date date = new Date(time);
SimpleDateFormat formatter = new SimpleDateFormat(format);
String daystr = formatter.format(date);
return daystr;
}
/**
* 是否存在sd卡
*
* @return 是否存在sd卡
*/
private static Boolean isExistsSD() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
/**
* 获得文件名
*
* @param context 上下文
* @return 文件名
*/
private static String getFileName(Context context) {
String fileName = getDate(System.currentTimeMillis(), "yyyyMMddHHmmss") + ".png";
final String localPath;
if (isExistsSD()) {
localPath = context.getExternalCacheDir() + File.separator + fileName;
} else {
localPath = context.getFilesDir() + fileName;
}
return localPath;
}
/**
* 截屏并保存
*
* @param a activity
* @return 保存的路径
*/
public static String shoot(Activity a) {
String localPath = getFileName(a);
boolean ret = AppScreenMgr.savePic(AppScreenMgr.takeScreenShot(a), localPath);
if (ret) {
return localPath;
} else {
return "";
}
}
/**
* 截屏并保存
*
* @param context 上下文
* @param webView webview
* @return 保存的路径
*/
public static String shootWebView(Context context, WebView webView) {
String localPath = getFileName(context);
boolean ret = AppScreenMgr.savePic(AppScreenMgr.captureWebView(webView), localPath);
if (ret) {
return localPath;
} else {
return "";
}
}
}